2016-09-14 37 views
0

假設我有數組PHP中的數組:陣列的PHP數組:得到值的列表給定屬性

$array = [ 
    ['value'=>1, 'other_attribute'=>'whatever'], 
    ['value'=>13, 'other_attribute'=>'whatever'], 
    ['value'=>45, 'other_attribute'=>'whatever'], 
    ['value'=>64, 'other_attribute'=>'whatever'], 
]; 

我怎樣才能得到一個包含每個數組的只是一個特定屬性的列表元素?於我而言,如果我想獲得「價值」的名單,輸出應該是這樣的:使用Laravel framework [1, 13, 45, 64]

,很容易與query builder對象,只是這樣做:$array->lists('value');。有沒有辦法在普通的PHP中做到這一點?

+0

並檢查得分最高的答案重複的,而不是簡單地接受的答案 –

+1

'array_column($陣列,「值」);' –

回答

1

當然,剛剛建立自己的循環來做到這一點:

$values = []; // the new array where we'll store the 'value' attributes 
foreach ($array as $a) { // let's loop through the array you posted in your question 
    $values[] = $a['value']; // push the value onto the end of the array 
} 
+1

'$ values = array_column($ array,'value');'更容易 –