5

我有一个 PHP 数组:

array
  0 => 
    array
      'cid' => string '18427' (length=5)
  1 => 
    array
      'cid' => string '17004' (length=5)
  2 => 
    array
      'cid' => string '19331' (length=5)

我想加入这些值,所以我可以得到这样的字符串:

18427,17004,19331

我尝试了implodePHP函数,但出现错误:

A PHP Error was encountered

如何只加入其中的值cid

4

3 回答 3

12

您必须首先映射数组值:

echo join(',', array_map(function($item) {
    return $item['cid'];
}, $arr));

如果没有闭包,它看起来像这样:

function get_cid($item)
{
  return $item['cid'];
}

echo join(',', array_map('get_cid', $arr));

也可以看看:array_map()

于 2013-04-02T07:37:45.910 回答
0

您可以遍历每个元素

$result = '';
foreach($array as $sub_array){
  $result .= $sub_array['cid'] . ',';
  }
$result = substr($result, -2);
于 2013-04-02T07:38:41.810 回答
0
// set a new variable for their the value of cid
$cid = array();

// then loop to call them one by one.
foreach($css as $style){
 foreach($style as $row){
    array_push($cid,$row);
 }
}

// then the new $cid array would contain the values of your cid array
 $cid = array('1','2','3');

注意:那么你现在可以内爆或淹没你想要获得你想要的数据集..

于 2013-04-02T07:47:41.277 回答