Transforming arrays into the correct formats I am still attempting to learn how to best handle arrays and iterate their values transforming them into the correct formats that are needed for my database/API.

So I have this array:

$arr = [
    0 =&gt; [
        'field' =&gt; [
            0 =&gt; [
                '@name' =&gt; 'first_name',
                '@value' =&gt; 'Jack'
            ],
            1 =&gt; [
                '@name' =&gt; 'last_name',
                '@value' =&gt; 'Lew'
            ],
            2 =&gt; [
                '@name' =&gt; 'email',
                '@value' =&gt; 'jack@gmail.com'
            ]
        ]
    ]
];


And I need it to look like this:

[0] =&gt; Array
    (
        [first_name] =&gt; Jack
        [last_name] =&gt; Lew
        [email] =&gt; jack@gmail.com
    )


To get to this I have taken the same approach I usually take:

for( $i = 0; $i &lt; count($arr); $i++ ) {
    foreach($arr[$i] as $k =&gt; $v) {
        print_r($k . ': \r\n');
        if( is_array($v) ) {
            foreach($v as $key =&gt; $val) {
                $new[$i][$val['@name']] = $val['@value'];
            }
        }
    }
}


The problem I having here is, I believe there has to be a better way to achieve the same results without being three levels deep of loops, since they are slower and just harder to debug altogether.

I would appreciate any insights on how I can make this code better in both readability and performance.
If you know that the key field exists, you could use array_map() to apply a function on each row. This function could use array_column() to transform a column as keys, and another as values:

$arr = [
    [
        'field' =&gt; [
            ['@name' =&gt; 'first_name', '@value' =&gt; 'Jack'],
            ['@name' =&gt; 'last_name', '@value' =&gt; 'Lew'],
            ['@name' =&gt; 'email', '@value' =&gt; 'jack@gmail.com']
        ]
    ],
    [
        'field' =&gt; [
            ['@name' =&gt; 'first_name', '@value' =&gt; 'John'],
            ['@name' =&gt; 'last_name', '@value' =&gt; 'Doe'],
            ['@name' =&gt; 'email', '@value' =&gt; 'John@gmail.com']
        ]
    ]
];

$data = array_map(function($item) {
    return array_column($item['field'], '@value', '@name');
}, $arr) ;

print_r($data);


Output:

Array
(
    [0] =&gt; Array
        (
            [first_name] =&gt; Jack
            [last_name] =&gt; Lew
            [email] =&gt; jack@gmail.com
        )

    [1] =&gt; Array
        (
            [first_name] =&gt; John
            [last_name] =&gt; Dow
            [email] =&gt; John@gmail.com
        )

)