i need to get array value for key2 so instead of
key2: "value2-bonusValue"
i need to get it like this
key2: {
"value2",
"bonusValue"
}
second if i removed : from one of arrays i get Undefined offset: 1 i need to be able to get array without key and key with multikeyes
$my_string = "key0:value0,key1:value1,key2:value2-bonusValue";
$convert_to_array = explode(',', $my_string);
foreach($convert_to_array as $_array){
$temp = explode(":", $_array);
$arrays[$temp[0]] = $temp[1];
}
return $arrays;
You are missing just one set, the splitting of the sub elements. In this code it splits the value by -
, but if there is only 1 element then it sets it back to being the first element, otherwise it adds the sub array in...
$convert_to_array = explode(',', $my_string);
foreach($convert_to_array as $_array){
$temp = explode(":", $_array);
$sub = explode("-", $temp[1]);
if ( count($sub) == 1 ) {
$sub = $sub[0];
}
$arrays[$temp[0]] = $sub;
}
print_r( $arrays );
which gives...
Array
(
[key0] => value0
[key1] => value1
[key2] => Array
(
[0] => value2
[1] => bonusValue
)
)
For a missing :
you can check the number of elements in $temp
...
$convert_to_array = explode(',', $my_string);
foreach($convert_to_array as $_array){
$temp = explode(":", $_array);
if ( count($temp) == 2 ) {
$sub = explode("-", $temp[1]);
if ( count($sub) == 1 ) {
$sub = $sub[0];
}
$arrays[$temp[0]] = $sub;
}
}
Not sure what you want to do with the value though.