1

I have an array of array that looks like this:

Array
(
    [0] => Array
        (
            [id] => I100
            [name] => Mary
            [gender] => F
        )
    [1] => Array
        (
            [id] => I101
            [name] => John
            [gender] => M
        )
    [2] => Array
        (
            [id] => I245
            [name] => Sarah
            [gender] => F
        )
)

I want to set the key of the parent array with the value of id, so the result array looks like this:

Array
(
    [I100] => Array
        (
            [id] => I100
            [name] => Mary
            [gender] => F
        )
    [I101] => Array
        (
            [id] => I101
            [name] => John
            [gender] => M
        )
    [I245] => Array
        (
            [id] => I245
            [name] => Sarah
            [gender] => F
        )
)

If possible I'd like to avoid using an additional loop to go through the array and creating a new array to store each item with the proper key, as the array can have thousands of items.

Thanks in advanced!

Mike Feng
  • 803
  • 2
  • 9
  • 19

3 Answers3

2

Despite your caveat, a loop is the obvious solution:

$newArray = [];
foreach($oldArray as $item)
    $newArray[$item['id']] = $item;

If the problem you have is not specifically with a loop, but rather creating a copy of the array is causes excessive memory consumption, then you can edit the array in place, with a for loop:

for($i=0; $i<count($oldArray); $i++){
    $oldArray[$oldArray[$i]['id']] = $oldArray[$i];
    unset($oldArray[$i]);
}

Note this works because the id elements are alphanumeric strings, if they where simple integars then the above code could overwrite sections.

The only other solution is to build the correct array in the 1st place, in a similar manner.

For example, using PDO::fetch instead of PDO::fetchAll:

//$newArray = $sth->fetchAll(PDO::FETCH_ASSOC);
$newArray = [];
while($row = $sth->fetch(PDO::FETCH_ASSOC))
    $newArray[$row['id']] = $row;
Steve
  • 20,703
  • 5
  • 41
  • 67
  • The code that created the original array cannot be altered. – Mike Feng Jan 25 '16 at 13:14
  • OK, well then you have to loop. I just made an edit to give a memory friendly option if thats your actual issue with looping. If thats not, then you need to clarify **why** you dont want to use a loop – Steve Jan 25 '16 at 13:20
1

You can't overwrite keys while iterating through an array "on the fly". So here is solution with array_map which produces an array with needed structure:

// assuming $arr is your initial array
$result = [];

array_map(function($a) use (&$result){
    $result[$a['id']] = $a;
}, $arr);

// $result contains the needed array
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Note this still creates a copy of the array, and is slower than a regular foreach loop, but as the OP has not specified **why** they dont want to use a loop, this is technically a reasonable answer – Steve Jan 25 '16 at 13:25
  • I also wish to know how someone is going to change all keys in initial array on the fly without loop and creating new array – RomanPerekhrest Jan 25 '16 at 13:32
0

You can add needed key during creation of this array.

Saleniex
  • 615
  • 5
  • 4