Joe M.
Joe M.

Reputation: 1054

Reorganize an array by 'id' index of nested arrays

I have an array that looks like this:

Array([0]=>Array([id]=>7 [name]=foo) [1]=>Array([id]=>10 [name]=bar) [2]=>Array([id]=>15 [name]=baz))

Each index contains an another array with various elements including an 'id'. I would like to "go up" a level, such that my top-level array is indexed by the ID element of the corresponding nested arrays, but that index still contains an array with all of the elements that were in the sub arrays?

In other words, how can I use PHP to turn the above array into this:

Array([7]=>Array([id]=>7 [name]=foo) [10]=>Array([id]=>10 [name]=bar) [15]=>Array([id]=>15 [name]=baz))

Upvotes: 1

Views: 931

Answers (4)

mickmackusa
mickmackusa

Reputation: 48001

In modern, supported versions of PHP, this whole task can be achieved with array_column() alone.

Using null as the second parameter will leave the rows unchanged.

Using id as the 3rd parameter will assign those columnar values as the new first level keys. Be aware that if these columnar values are not unique, subsequently encountered duplicates will overwrite previously encountered rows with the same id value -- this is because keys cannot be duplicates on a given level in an array.

DO NOT bother calling array_combine(), it is simply unnecessary/indirect.

Code: (Demo)

$array = [
    ['id' => 7, 'name' => 'foo'],
    ['id' => 10, 'name' => 'bar'],
    ['id' => 15, 'name' => 'baz'],
];

var_export(
    array_column($array, null, 'id')
);

Output:

array (
  7 => 
  array (
    'id' => 7,
    'name' => 'foo',
  ),
  10 => 
  array (
    'id' => 10,
    'name' => 'bar',
  ),
  15 => 
  array (
    'id' => 15,
    'name' => 'baz',
  ),
)

Upvotes: 1

ᴍᴇʜᴏᴠ
ᴍᴇʜᴏᴠ

Reputation: 5266

Since PHP 5.5.0, you can shorten the code by using array_column() instead of array_map().

$result = array_combine(array_column($array, 'id'), $array);

Upvotes: 0

Jon
Jon

Reputation: 437634

What you need to do here is extract the ids from each sub-array in your input. If you have these as an array of ids, you are just an array_combine call away from re-indexing your original array to use these ids as the keys.

You can produce such an array of ids using array_map, which leads to:

// input data
$array = array(array('id' => '7', 'name' => 'foo'),array('id' => 10, 'name' => 'bar'));

// extract ids from the input array
$ids = array_map(function($arr) { return $arr['id']; }, $array);

// "reindex" original array using ids as array keys, keep original values
$result = array_combine($ids, $array);

print_r($result);

The syntax I 've used for the anonymous function (first argument to array_map) requires PHP >= 5.3, but you can achieve the same (although a bit less conveniently) with create_function in any PHP version you 'd not be ashamed of using.

See it in action.

Upvotes: 4

Cyclonecode
Cyclonecode

Reputation: 30071

Try this:

 $newArray = array();
 foreach($oldArray as $key => $value) {
    $newArray[$value['id']] = $value;
 }

Upvotes: 1

Related Questions