怎样把以下这样的数组
Array
(
[0] => Array
(
[device_token] => Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu
)
[1] => Array
(
[device_token] => AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV
)
)
封装成
"device_tokens":"device1,device2,…",
这种形式
PHP中文网2017-04-10 17:05:48
$array = array(
array(
'device_token' => 'Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu'
),
array(
'device_token' => 'AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV'
)
);
$result = [];
array_walk_recursive($array, function ($value, $key) use (&$result) {
$result[$key][] = $value;
});
foreach ($result as $key => $value) {
$result[$key] = implode($value, ',');
}
$result = json_encode($result);
// 如果你确定你不是想要json格式的话就保留下面
$result = substr($result, 1, -1);
大家讲道理2017-04-10 17:05:48
//php5.5
$array = array(
array(
'device_token' => 'Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu'
),
array(
'device_token' => 'AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV'
)
);
//如果字段不固定的话参考楼上
$result = array_column($array,'device_token');
迷茫2017-04-10 17:05:48
$out=['devices'=>''];
array_map(function($arr) use (&$out){
$value=array_values($arr)[0];
$out['devices'].=($out['devices'])?',':'';
$out['devices'].=$value;
},$array);
print_r($out);
Array
(
[devices] => Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu,AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV
)