我有一个二维数组,格式如下所示。我如何使用implode()
仅输出user_id
,以便最终结果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
)
)
PHP 5.5:
$result = join(',', array_column($data, 'user_id'));
5.3<=PHP<=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"];')));
$nested_array = array();
foreach($array as $a) {
$nested_array[] = $a['user_id'];
}
$implode = implode(',',$nested_array);
或
$implode = '';
foreach($array as $a) {
$implode .= $a['user_id'] . ',';
}
$implode = substr($implode,-1);
你可以试试这个:
foreach ($array as $row)
{
$values[] = $row['user_id'];
}
$user_id= implode(',', $values);
echo $user_id;
?>
您也可以尝试:
implode(', ', array_map(function($k){ return $k['user_id'];}, $arr));