PHP Array in to comma separated list

前端 未结 4 944
灰色年华
灰色年华 2021-01-14 04:42

I have a list of items from a database being parse in an array

Array
(
    [0] => Array
        (
            [dToken] => 731fea87ea9dc61f16e93f1ad2b96         


        
相关标签:
4条回答
  • 2021-01-14 05:11

    You're just about there.

    $tokens = array();
    foreach($result as $device){
        $tokens[] = $device['dToken'];
    }
    echo implode(',', $tokens);
    
    0 讨论(0)
  • 2021-01-14 05:19
    callback = function($row){
        return $row['dToken'];
    }
    
    implode(",",array_map(callback,$result));
    
    0 讨论(0)
  • 2021-01-14 05:32

    You could just build the string:

    $cvsString = '';
    $delimiter = '';
    foreach($result as $device){
        $cvsString.= $delimiter . $device['dToken'];
    
        $delimiter = ',';
    }
    
    var_dump($cvsString);
    

    Or you could first build in array:

    $cvsArray = array();
    foreach($result as $device){
        $cvsArray[] = $device['dToken'];
    }
    
    var_dump(implode(',', $cvsArray));
    
    0 讨论(0)
  • 2021-01-14 05:33

    To answer the question corresponding to the tile (transform an Array to a list with separator) use the implode function. To generate a CSV I would google php CSV, I'm sure there are already lots of function to do it.

    0 讨论(0)
提交回复
热议问题