我有一个二维数组,格式如下所示。如何使用user_id
仅输出implode()
,以便最终结果为1705,1757,1832
?
Array (
[0] =>
Array (
[nickname] => picachu
[user_id] => 1705
[name] => picachu .jpg
[city_name] => pallet town
)
[1] =>
Array (
[nickname] => charmander
[user_id] => 1757
[name] => charmander.jpg
[city_name] => verivian city
)
[2] =>
Array (
[nickname] => squaretle
[user_id] => 1832
[name] => squaretle.jpg
[city_name] => Celadon
)
)
答案 0 :(得分:2)
PHP 5.5:
$result = join(',', array_column($data, 'user_id'));
5.3< =&PHP LT = 5.4:
$result = join(',', array_map(function($item)
{
return $item['user_id'];
}, $data));
PHP< 5.3:
$result = join(',', array_map(create_function('$item', 'return $item["user_id"];')));
答案 1 :(得分:1)
$nested_array = array();
foreach($array as $a) {
$nested_array[] = $a['user_id'];
}
$implode = implode(',',$nested_array);
OR
$implode = '';
foreach($array as $a) {
$implode .= $a['user_id'] . ',';
}
$implode = substr($implode,-1);
答案 2 :(得分:1)
你可以试试这个:
foreach ($array as $row)
{
$values[] = $row['user_id'];
}
$user_id= implode(',', $values);
echo $user_id;
?>
答案 3 :(得分:1)
您也可以尝试:
implode(', ', array_map(function($k){ return $k['user_id'];}, $arr));