数组内爆在php中

时间:2013-01-02 05:23:32

标签: php arrays implode

我有一个以下格式的数组:

$var = Array
     ( 
     [0] => Array
         (
        [name] => Harry
          )

      [1] => Array
         (
        [name] => Wayne
          )

      )
 Array
     (
     [0] => Array
         (
        [name] => Wayne
         )

我想破坏这个数组,以便我得到格式:

 Harry,Wayne
 Wayne

从我的尝试中我得到的格式是:

Harry,Wayne
Harry,Wayne,Wayne

我尝试过什么(不重要)

foreach($var as $a){
 foreach($a as $b){
 }$c[] = $b
   }
 $imp = implode(',',$c);

使用fetch_array从数据库中获取$ var。

    $this->db->select('name');
    $this->db->where('id', $Id);
    $this->db->from('info');
    $row = $this->db->get();
    $var = $row->result_array();

其中$ Id是包含特定用户ID的数组。

3 个答案:

答案 0 :(得分:1)

试试这个。

foreach($var as $a){
  $m = '';
  $delim = '';
  foreach($a as $k){
     $m .= $delim . $k['name'];
     $delim = ',';
  }

  $c[] = $m;
}

foreach($c as $d){
  echo $d;
}

答案 1 :(得分:1)

foreach($var as $a)
{
    unset($temp);
    foreach($a as $b)
    {
        $temp[] = $b['name'];
    }
    $c[] = implode(",", $temp);
}

// output all the names
foreach ($c as $csvNames)
{
    echo $csvNames;
}

答案 2 :(得分:0)

请忽略那些硬编码的循环。它有一个递归函数。

array_walk_recursive($var, create_function('$val, $key', 'array_push($obj, $val);'), &$output);
echo implode(",",$output);