1

我有一个如下的数组,我想在一个单独的数组中获取 'id' 前面的所有值。比如:arry('12', '10', '11', '9')

 array
      3 => 
        array
          'occurance' => int 4
          'id' => string '12' (length=2)
      1 => 
        array
          'occurance' => int 3
          'id' => string '10' (length=2)
      2 => 
        array
          'occurance' => int 2
          'id' => string '11' (length=2)
      0 => 
        array
          'occurance' => int 1
          'id' => string '9' (length=1)
4

5 回答 5

7

您可以使用array_map

$values = array(
        3 => array('occurance' => 4,'id' => '12'),
        1 => array('occurance' => 3,'id' => '10'),
        2 => array('occurance' => 2,'id' => '11'),
        0 => array('occurance' => 1,'id' => '9'));

$values = array_map(function($var){ return $var['id']; }, $values);
var_dump($values);

输出

array
  3 => string '12' (length=2)
  1 => string '10' (length=2)
  2 => string '11' (length=2)
  0 => string '9' (length=1)
于 2012-10-05T11:42:01.747 回答
2

使用 foreach 循环并放入id另一个数组:

$newArray = array()
foreach($array as $val){
    $newArray[] = $val['id'];
}
于 2012-10-05T11:34:58.290 回答
1

试试这个:

foreach($YourArray as $ar)
{
    $FinalArray[] = $ar['id'];
}

print_r($FinalArray);
于 2012-10-05T11:35:50.767 回答
0
foreach($array as $key=>$value)
{
if(array_key_exists('id',$value))
$arr2[]=$value['id'];
}
于 2012-10-05T11:38:47.553 回答
0

从 PHP 5.5 开始,您可以使用array_column

$idArray = array_column($multiArray, 'id');
于 2016-11-11T22:01:31.900 回答