If I have an array $array
like this:
Array (
[0] => Array (
[id] => 11
[name] => scifi
)
[1] => Array (
[id] => 12
[name] => documetary
)
[2] => Array (
[id] => 10
[name] => comedy
)
)
How could I turn it into simply:
Array ( 11, 12, 10 )
with no key value pairs.
I am trying to extract only the id
from each array and add them into 1 array. I am trying it with a foreach;
$ids = [];
if ( $array ) {
foreach ( $array as $item ) {
$ids[] = $term->id;
}
}
print_r($ids);
It just returns 3 empty arrays Array ( [0] => [1] => [2] => )
CodePudding user response:
Using a loop, you can do this:
$arr1 = [
['id'=>11,'name'=>'scifi'],
['id'=>12,'name'=>'documentry'],
['id'=>10,'name'=>'comedy'],
];
$arr2 = [];
foreach($arr1 as $internal){
array_push($arr2,$internal['id']);
}
print_r($arr2);
Here we access all internal array's ID's and insert them into a new array.