Anil Astrio Anil Astrio - 24 days ago 9
PHP Question

Convert multidimensional array

How can I convert the below array to look like the one right below. I am trying to use array_map but i am confused on how to write the function.

array(3) {
["award_year"]=>
array(2) {
[0]=>
string(7) "1999-01"
[1]=>
string(7) "2010-02"
}
["award_title_user"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(2) "tt"
}
["award_description_user"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(3) "ddd"
}
}


This what i am trying to achieve:

array(2) {
[0]=>
array(3) {
["award_year"]=>
string(7) "2010-02"
["award_title_user"]=>
string(2) "tt"
["award_description_user"]=>
string(3) "ddd"
}
[1]=>
array(3) {
["award_year"]=>
string(7) "1999-01"
["award_title_user"]=>
string(1) "2"
["award_description_user"]=>
string(1) "2"
}
}

Answer Source
$newArr = [];
foreach($oldArr as $key=>$row) {
   foreach($row as $index=>$item) {
       $newArr[$index][$key] = $item;
   }   
}

this will solve it, but no checks if data is valid as you mentioned