Assign value of nested array as array key
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.