How to extract specific array keys and values to another array?

PHP 5.5 introduced an array function that does exactly what you want. I'm answering this in hopes that it may help someone in future with this question.

The function that does this is array_column. To get what you wanted you would write:

array_column($oldArray, 'type', 'id');

To use it on lower versions of PHP either use the accepted answer or take a look at how this function was implemented in PHP and use this library: https://github.com/ramsey/array_column


Just use a good ol' loop:

$newArray = array();
foreach ($oldArray as $entry) {
    $newArray[$entry['id']] = $entry['type'];
}

Tags:

Php

Arrays