An issue with the PHP array to the string

Source: Internet
Author: User
How to put an array such as the following

Array(    [0] => Array        (            [device_token] => Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu        )    [1] => Array        (            [device_token] => AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV        ))

Encapsulated into

"device_tokens":"device1,device2,…", 这种形式

Reply content:

How to put an array such as the following

Array(    [0] => Array        (            [device_token] => Al9_G0i6ftf7fvkKsGM9o6jN5iyoqt8zTjcsh_kw6HUu        )    [1] => Array        (            [device_token] => AmfUS3qeXKrJt1K1ZTICiD-ED6a_YgM3GdBRp6gR4RgV        ))

Encapsulated into

"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

)

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.