我有如下格式的数组:
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()
从数组中抓取第一列,这样你就可以得到一个数组,例如
Array (
[0] => 6
[1] => 8
[2] => 25
[3] => 34
)
然后implode()
就像你已经做的那样,例如
echo implode(",", array_column($data["sales"], 1));
输出: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);