怎样把以下这样的数组
array( [0] => array ( [device_token] => al9_g0i6ftf7fvkksgm9o6jn5iyoqt8ztjcsh_kw6huu ) [1] => array ( [device_token] => amfus3qexkrjt1k1zticid-ed6a_ygm3gdbrp6gr4rgv ))
封装成
device_tokens:device1,device2,…, 这种形式
回复内容: 怎样把以下这样的数组
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
)