phparraysmultidimensional-array

Convert two columns of a 2d array into key=>value pairs


I have the following array

Array
(
    [0] => Array
        (
            [id] => 5
            [name] => 44
        )

    [1] => Array
        (
            [id] => 9
            [name] => 55
        )

)

i need to convert it to

array( '5' => '44', '9' => '55' ) 

i tried all function of arrays from php.net but i could not figure out how to do it


Solution

  • did you try foreach?

    $result = array();
    foreach($yourarray as $entry) {
      $result[$entry['id']] = $entry['name'];
    }