How to remove level 1 keys using only one Callback method

2

I do not want to use foreach and for . For example, I have the following array structure:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id_usuario] => 1
                )

            [1] => Array
                (
                    [idade] => 21
                )
        )
    [1] => Array
         (
            [0] => Array
                (
                    [id_usuario] => 2
                )

            [1] => Array
                (
                    [idade] => 15
                )
         )
)

But I would like my array to look like this:

 Array
    (
        [0] => Array
            (
                [id_usuario] => 1                       
                [idade] => 21
            )
        [1] => Array
            (
               [id_usuario] => 2
               [idade] => 15
            )
    )

Note: I also do not want anything similar to this:

foreach ($items as list($var1, $var2, $var3)) {
    if ($var1 == $var2) {
        echo $var3;
    }
}

The goal is to improve the structure.

    
asked by anonymous 27.10.2015 / 20:24

2 answers

0

I did it like this:

$myarray = array(
            array(
                array('id_usuario' => 1),
                array('idade' => 21)
                ),
            array(
                array('id_usuario' => 2),
                array('idade' => 15)
                )
         );
$data =  array_map(function($arr) {
                 return array_merge($arr[0],$arr[1]);
}, $myarray);

echo '<pre>';
print_r($data);
    
27.10.2015 / 21:41
1

As you do not want to use conditions, considering its fixed array structure, array_merge resolves, it merges one or more arrays.

$teste = Array(
            Array(
                Array('id_usuario' => 1),
                Array('idade' => 21)),
            Array(
                Array('id_usuario' => 2),
                Array('idade' => 15)));

$r[0] = array_merge($teste[0][0], $teste[0][1]);
$r[1] = array_merge($teste[1][0], $teste[1][1]);

print_r($r);
    
27.10.2015 / 21:10