Why does the operator
$array['country'][] return what logically would be $array[]['country']?
What I am saying is this. If you want to extract from a MySQL array, the value of [‘country’] for every row, [1],[2]…[n], you have to use
$array['country'][]
despite fact that they are ordered as
$array['row#']['country']
Is this because PHP is reading something backwards, or because I am just lacking some fundamental array information?
FULL-ish code here
$result = array();
foreach($data as $value){
$array['country'][] = $value['country'];
$array['report'][] = $value['report'];
}
$data = $array;
Let me know if I am just dumb… I can’t really grasp why this is working this way.
It is because you are constructing the array in that way:
If you use
$array['country'][] = $value['country'];, you are adding a new value to the sub-array which is part of the containing array under thecountrykey. So it will be mapped to$array['country'][], you cannot expect otherwise.If you want it to map to
array[]['country'], then (using part of code from@Lawrence’s answer), you’d have to add the new values using explicit numerical indexes as the key: