PHP7如何压扁多维数组?

时间:2018-01-14 16:26:40

标签: php arrays

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [itemid] => 2367
                    [price] => 15.99
                    [timestamp] => 2018-01-14 16:23:03
                    [qty] => 22
                )

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

                   [itemid] => 2368
                    [price] => 15.99
                    [timestamp] => 2018-01-14 16:23:01
                    [qty] => 13
            )

    )

等...

你可以看到1级数组包含另一个无用的数组...如何展平它而不是嵌套数组我有这个结果:

Array
(
    [0] => Array
        (
              [itemid] => 2367
              [price] => 15.99
              [timestamp] => 2018-01-14 16:23:03
              [qty] => 22


        )
    [1] => Array
    (


           [itemid] => 2368
           [price] => 15.99
           [timestamp] => 2018-01-14 16:23:01
           [qty] => 13


    )

1 个答案:

答案 0 :(得分:0)

您可以尝试任何类似array_map的循环,它会返回第一个元素,如$elem[0]

<?php
$multi = [[[1,2]],[[3,4]],[[5,6]]];
$flat = array_map(function($e){return $e[0];},$multi);
print_r($flat);
?>

现场演示:https://eval.in/934753