2

I have a array with x array in it with always same keys in these arrays .

Array
(
    [foo] => Array
        (
            [q] => "12r3"
            [w] => "3r45"
            [e] => "67e8"
        )
    [foo1] => Array
        (
            [q] => "féEt"
            [w] => "Ptmf"
            [e] => "4323"
        )
    [foo2] => Array
        (
            [q] => "axa"
            [w] => "dfvd"
            [e] => "hjh"
        )
)

and I need to merge all these arrays in one like:

[foo] => Array
(
     [q] => Array
       (
          [0] => "12r3",
          [1] => "féEt",
          [2] => "axa",
       )
     [w] => Array
       (
          [0] => "3r45",
          [1] => "Ptmf",
          [2] => "dfvd",
       )
    ...

How can I do that?

Thanks.

0

3 Answers 3

1

This should work for you:

(Here I just go through the first array and grab all columns with array_column() from the entire array with the keys from the first array.)

<?php

    $arr = [

        "foo" => [
            "q" => "12r3",
            "w" => "3r45",
            "e" => "67e8"
        ],
        "foo1" => [
            "q" => "féEt",
            "w" => "Ptmf",
            "e" => "4323"
        ],
        "foo2" => [
            "q" => "axa",
            "w" => "dfvd",
            "e" => "hjh"
        ]
    ];


foreach($arr["foo"] as $k => $v) {
    $results[$k] = array_column($arr, $k);
}

print_r($results);

?>

Output:

Array
(
    [q] => Array
        (
            [0] => 12r3
            [1] => féEt
            [2] => axa
        )

    [w] => Array
        (
            [0] => 3r45
            [1] => Ptmf
            [2] => dfvd
        )

    [e] => Array
        (
            [0] => 67e8
            [1] => 4323
            [2] => hjh
        )

)
Sign up to request clarification or add additional context in comments.

4 Comments

@Smashou But as you can see it works and the output is correct
When I run you code I just have Array ( [q] => Array ( [0] => 12r3 ), [w] => Array ( [0] => 3r45 ), [e] => Array ( [0] => 67e8 )
@Smashou php under 5.5?
Yeah it works! thanks I didn't know the function array_column() .
0
$newArray = [];
foreach($array as $value) {
    foreach($value as $key => $data) {
        $newArray[$key][] = $data;
    }
}
var_dump($newArray);

or

$newArray = [];
foreach($array as $value) {
    $newArray = $newArray + $data;
}
var_dump($newArray);

Comments

0

If you are using PHP 5.5+ then use array_column, otherwise array_map.

Using array_map:
<?php

$a = Array
(
    0 => Array
        (
            'q' => "12r3",
            'w' => "3r45",
            'e' => "67e8",
        ),
    1 => Array
        (
            'q' => "féEt",
            'w' => "Ptmf",
            'e' => "4323",
        ),
    2 => Array
        (
            'q' => "axa",
            'w' => "dfvd",
            'e' => "hjh",
        )
);

$b = array (
'q'=>array_map(function($element){return $element['q'];}, $a),
'w'=>array_map(function($element){return $element['q'];}, $a),
'e'=>array_map(function($element){return $element['q'];}, $a),
);

print_r($b);    
?>

Using array_column:

<?php

$a = Array
(
    0 => Array
        (
            'q' => "12r3",
            'w' => "3r45",
            'e' => "67e8",
        ),
    1 => Array
        (
            'q' => "féEt",
            'w' => "Ptmf",
            'e' => "4323",
        ),
    2 => Array
        (
            'q' => "axa",
            'w' => "dfvd",
            'e' => "hjh",
        )
);

$b = array (
'q'=>array_column('q', $a),
'w'=>array_column('w', $a),
'e'=>array_column('e', $a),
);

print_r($b);    
?>

Output:
Array
(
[q] => Array
    (
        [0] => 12r3
        [1] => fÚEt
        [2] => axa
    )

[w] => Array
    (
        [0] => 12r3
        [1] => fÚEt
        [2] => axa
    )

[e] => Array
    (
        [0] => 12r3
        [1] => fÚEt
        [2] => axa
    )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.