I am using php 7.1 and have the following array:
However, I would simply like to have the following array:
array(3808, 3807, 3806, 3805)
Any suggestions how to convert it to array?
Appreciate your replies!
I am using php 7.1 and have the following array:
However, I would simply like to have the following array:
array(3808, 3807, 3806, 3805)
Any suggestions how to convert it to array?
Appreciate your replies!
array_column() function will help:
$ids = array_column($records, 'ID');
print_r($ids);
Will output just array of ID's.
Check docs for it Docs
0 => 3385 etc.Convert the Object to an Array and use array_column to return an array of values from a specific key.
$a = (object) [
0 => [ 'ID' => 3808],
1 => [ 'ID' => 3807],
2 => [ 'ID' => 3806],
3 => [ 'ID' => 3805],
];
$b = array_column((array)$a, 'ID');
// $b = [3808,3807,3806,3805]
Note: (array)$a enforces Array conversion. Conversely, (object)$a will convert an Array to a stdClass Object.
(array)$a seems redundant in PHP's context.array_column is an Object you will get the error, 'array_column() expects parameter 1 to be array, object given in...'. You must convert an Object to an Array to use array_column.ID is a property of an object that exists and not some key in an array.