合并具有相同键的数组

时间:2015-03-01 15:18:13

标签: php arrays merge

我有一个带有x数组的数组,在这些数组中总是使用相同的键。

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"
        )
)

我需要将所有这些数组合并为:

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

我该怎么做?

感谢。

3 个答案:

答案 0 :(得分:1)

这应该适合你:

(这里我只是浏览第一个数组,并使用第一个数组中的键从整个数组中获取array_column()的所有列。)

<?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);

?>

输出:

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
        )

)

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

如果您使用的是PHP 5.5+,则使用array_column,否则使用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
    )

)