从数组值获取内联结果

时间:2018-11-30 15:23:09

标签: php arrays

下面有两个数组:

第一个数组:

[0] => 051, 052, 053.
[1] => 061, 062.

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => book
                    [number] => 41
                )

            [1] => Array
                (
                    [name] => pencil
                    [number] => 42
                )

        )

    [1] => Array
        (
            [name] => eraser
            [number] => 71
        )

)

我想从上面的第一个数组中输出

[0] => 41, 42.
[1] => 71.

我想从上面的第二个数组进行输出

{{1}}

请告知。谢谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试这样

$arr = Array (
    Array (
            Array (
                    "name" => "one",
                    "number" => "051"
                ),
            Array  (
                    "name" => "two",
                    "number" => "052"
                ),
            Array (
                    "name" => "three",
                    "number" => "053"
                )
        ),

    Array (
            Array (
                    "name" => "four",
                    "number" => "061"
                ),
            Array  (
                    "name" => "five",
                    "number" => "062"
                )
        )
);

foreach ($arr as $k => $s_arr) {
    echo "[" . $k . "] => ";
    foreach ($s_arr as $k2 => $v2) {
        echo $v2["number"] . " ";
    }

    echo "\n";
}

答案 1 :(得分:0)

您可以通过两个foreach()循环来尝试这种方式。

$numbers = [];
foreach ($array as $k => $v) {
    $num = [];
    foreach ($v as $k2 => $v2) {
       $num[] = $v2['number']; 
    }
     $numbers[$k] = implode(',',$num).'.';
}

print_r($numbers);

演示: https://3v4l.org/mEeO7