怎样把以下这样的数组
Array ( [0] => Array ( [device_token] => Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu ) [1] => Array ( [device_token] => AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV ) )
封装成
"device_tokens":"device1,device2,…", 这种形式
光阴似箭催人老,日月如移越少年。
$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);
//php5.5
$array = array( array( 'device_token' => 'Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu' ), array( 'device_token' => 'AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV' ) ); //如果字段不固定的话参考楼上 $result = array_column($array,'device_token');
json_encode(array) JSON
$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
)
//php5.5
json_encode(array) JSON
Array
(
)