如何显示从多维数组中选择的内容

时间:2018-12-12 03:30:59

标签: php arrays multidimensional-array

我有一个多维数组,其中两个数组具有多个数组,一个数组具有单个值,如下所示:

    $array([0] => array(
    [0] => array(
        ['name'] => 'one',
        ['number'] => 051
    ),
    [1] => array(
        ['name'] => 'two',
        ['number'] => 052
    )
),
[1] => array(
    ['name'] => 'three',
    ['number'] => 053
),
[2] => array(
    [0] => array(
        ['name'] => 'four',
        ['number'] => 054
    ),
    [1] => array(
        ['name'] => 'six',
        ['number'] => 055
    )

)

从上面的数组中,如何获得结果:

[0] = 051, 052.
[1] = 053.
[2] = 054, 055.

请帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

$var = array(
        array(
            array(
                'name' => 'one',
                'number' => '051'
            ),
            array(
                'name' => 'two',
                'number' => '052'
            )
        ),
        array(
            'name' => 'three',
            'number' => '053'
        ),
        array(
            array(
                'name' => 'four',
                'number' => '054'
            ), array(
                'name' => 'six',
                'number' => '055'
            )
            ));
    $numbers = [];
    foreach ($var as $k => $v) {
        $num = [];
            if(isset($v['number'])){
                $num[] = $v['number'];
            }
            foreach ($v as $k2 => $v2) {
                if(isset($v2['number'])){
                $num[] = $v2['number'];
                }
            }

        $numbers[$k] = implode(',', $num) . '.';
    }