在二维数组php中转换多数组

时间:2017-03-03 07:07:10

标签: php arrays type-conversion

我想在二维数组中转换一个多数组。我有以下多阵列的结果。

Array
(
[1] => Array
    (
        [0] => Array
            (
                [0] => Id
                [1] => Name
                [2] => Fname
                [3] => School
                [4] => Photo
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [0] => 32
                [1] => kamal
                [2] => hjhbg
                [3] => hnp
                [4] => B612_16.jpg
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [0] => 33
                [1] => dg
                [2] => fa
                [3] => f
                [4] => bg.jpg
            )

    )

[4] => Array
    (
        [0] => Array
            (
                [0] => 35
                [1] => mohit
                [2] => bc
                [3] => jhbvj
                [4] => B612.jpg
            )

    )

  )

现在我需要以下面的格式转换这个数组。

Array
(
[0] => Array
    (
                [0] => Id
                [1] => Name
                [2] => Fname
                [3] => School
                [4] => Photo

    )

[1] => Array
    (
                [0] => 32
                [1] => kamal
                [2] => hjhbg
                [3] => hnp
                [4] => B612_16.jpg

    )

[2] => Array
    (
                [0] => 33
                [1] => dg
                [2] => fa
                [3] => f
                [4] => bg.jpg

    )

[3] => Array
    (
                [0] => 35
                [1] => mohit
                [2] => bc
                [3] => jhbvj
                [4] => B612.jpg
    )

    )

3 个答案:

答案 0 :(得分:1)

试试这个

function array_to1d($a) {
    $out = array();
    foreach ($a as $b) {
        foreach ($b as $c) {
            if (isset($c)) {
                $out[] = $c;
            }
        }
    }
    return $out;
}
    echo "<pre>"; print_r(array_to1d($array)); // $array your array name

答案 1 :(得分:0)

你必须要做

$two_d_array = array_values($three_d_array);

答案 2 :(得分:0)

最短的解决方案是在{}使用array_walk()

array_walk($array, function(&$v) {
    $v = $v[0];
});