将二维数组的列转换为二维数组的行[重复]

问题描述 投票:0回答:3

我有一个包含 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",
       )
    ...

我该怎么做?

谢谢。

php arrays multidimensional-array transpose
3个回答
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
        )

)

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

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
    )

)
© www.soinside.com 2019 - 2024. All rights reserved.