I have an array like so:
Array (
[0] => - :description: Rate the Code
[1] => :long-description: ""
[2] => :points: !float 5
)
I would like to use PHP to change my array structure to look like this:
Array (
[- :description] => Rate the Code
[:long-description] => ""
[:points] => !float 5
)
Can anybody help me out with this? Here is what I have for code so far:
for ($j = 0; $j < sizeof($array[$i]); $j++) {
$pieces = explode(": ", $array[$i][$j]);
$key = $pieces[0];
$value = $pieces[1];
$array[$i][$j] = $array[$i][$key];
}
This code throws an Undefined index: - :description error for all of my indexes. The - :description changes in each error to the index that it is on however.
forloop, keep in mind that the conditional ($j < sizeof($array[$i])) is evaluated every iteration. To be much more efficient, you would evaluate the size of directly before the loop and then reference a new variable ($size) in the conditional.