I have an array called $data which each comprises of an array containing "listid" and "status" as illustrated below when looping through and var_dumping each element in $data:
array(2) { ["listid"]=> string(1) "0" ["status"]=> string(1) "0" }
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "2" }
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "4" }
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "0" }
array(2) { ["listid"]=> string(1) "0" ["status"]=> string(1) "6" }
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "1" }
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "0" }
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "2" }
I want to re-arrange this array to group by "listid". So the new array should look something like:
$new_array[0]["listid"]==17
$new_array[0]["status"]==array(2,4,0,1,0,2)
$new_array[1]["listid"]==0
$new_array[1]["status"]==array(0,6)
How can I re-arrange the original array to this format and is there a php function which could do this?
$data, assign values as required into your new array structure.listidas your array index on$new_arrayso you can target the assignment with ease. Usingarray_values()if you need to return to a 0,1,2 array index.