2012-10-05 95 views
1

我有一個數組,如下所示,我想在'id'前面的所有值中分隔數組。像:ARRY('12' , '10', '11', '9')php - 從關聯數組中提取1維數組

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

您可以使用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) 
1

使用foreach循環,把id在另一個數組:

$newArray = array() 
foreach($array as $val){ 
    $newArray[] = $val['id']; 
} 
1

試試這個:

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

print_r($FinalArray); 
0
foreach($array as $key=>$value) 
{ 
if(array_key_exists('id',$value)) 
$arr2[]=$value['id']; 
} 
0

自PHP 5.5,你可以使用array_column

$idArray = array_column($multiArray, 'id');