phparraysmultidimensional-arraytranspose

Reconfigure rows of a 2d array to be columns


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"
  }
}

Solution

  • $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