0

suppose i have an array of arrays in PHP:

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

how can i get a list containing just a specific attribute of each of the array's elements? in my case, if i wanted to get the list of 'value', the output should look like this: [1, 13, 45, 64]

using the Laravel framework, it's easy to do this with query builder objects, simply like this: $array->lists('value');. is there a way to do this in plain PHP?

szaman
  • 2,159
  • 1
  • 14
  • 30

1 Answers1

1

Sure, just build your own loop to do it:

$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
}
Hayden Schiff
  • 3,280
  • 19
  • 41