Assign value of nested array as array key I have a multi dimensional array like this:

[0] =&gt; 
    ['batch_id'] =&gt; '1'
    ['some_stuff'] =&gt; 'values'
    [0] =&gt;
        ['unit_id'] =&gt; '12'
        ['some_stuff'] =&gt; 'values'
    [1] =&gt;
        ['unit_id'] =&gt; '41'
        ['some_stuff'] =&gt; 'values'


This comes from an external API, so I have no control over the original formatting of the array.

What would be the preferred (least overhead) way to take all the 'unit_id' and 'batch_id' fields, remove them from the group of values about the item, and assign that value to the item's key?

What I've tried (and works, but I want to find something cleaner):

public function fixArray($batches) {
    $batchfix = array();
    foreach($batches as $batch) {
        $unitfix = $array;
        $batch_temp = $batch;
        array_shift(array_shift($batch_temp)); // Get rid of id details
        foreach($batch_temp as $unit)
            $unittemp = $unit;
            array_shift($unittemp);
            $unitfix[$unit['unit_id']] = $unittemp;
        $batchfix[$batch['batch_id']] = $batch;
    }
    return $batchfix;
}


Any suggestions?
I suppose the result you want to have is this:

    Array
    (
        [1] =&gt; Array
            (
                [stuff] =&gt; values
                [units] =&gt; Array
                    (
                        [12] =&gt; Array
                            (
                                [stuff] =&gt; values
                            )
                        [41] =&gt; Array
                            (
                                [stuff] =&gt; values
                            )
                    )
            )
    )


In this case you can use the code:

    public function fixArray($originalBatches) {
        $batches = array();
        foreach($originalBatches as $batch) {
            $units = array();
            $batch_id = $batch['batch_id'];
            unset($batch['batch_id']);
            foreach ($batch as $key =&gt; $value) {
                if (is_numeric($key) &amp;&amp; !empty($value['unit_id'])) {
                    $units[$value['unit_id']] = array_slice($value, 1);
                    unset($batch[$key]);
                }
            }
            $batches[$batch_id] = array_merge($batch, array('units' =&gt; $units));
        }
        return $batches;
    }


Still the use of the 2 foreach is not a very good practice. You can change the code accordingly to use array_walk. This would be much faster.