编程语言
首页 > 编程语言> > php – 如何从多维数组中仅内插()一列?

php – 如何从多维数组中仅内插()一列?

作者:互联网

我有以下格式的数组:

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

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

            [2] => Array
                (
                    [0] => 3
                    [1] => 25
                )

            [3] => Array
                (
                    [0] => 4
                    [1] => 34
                )

        )

)

使用:

foreach ($data['sales'] as $k => $row) {
    $list = implode(",",$row);
}

我得到以下输出:

1,62,83,254,34

但我只需要每个subArray的第二个值.预期结果必须是:

6,8,25,34

如何删除第一组值?

解决方法:

我喜欢array_column()但是如果你没有PHP> = 5.5.0:

$list = implode(',', array_map(function($v) { return $v[1]; }, $data['sales']));

或者与foreach:

foreach ($data['sales'] as $row) {
    $list[] = $row[1];
}
$list = implode(',', $list);

标签:implode,php,arrays,multidimensional-array
来源: https://codeday.me/bug/20190722/1504347.html