Reputation: 39
A certain php array give me this :
myarray{
0=>array { "key_01"=>"value_01", "key_02"=>"value_02"..."key_0n"=>"value_0n"}
1=>array { "key_11"=>"value_11", "key_12"=>"value_12"..."key_1n"=>"value_1n"}
.
.
.
n=>array { "key_n1"=>"value_n1", "key_n2"=>"value_n2"..."key_nn"=>"value_nn"}
}
And i would like to get this one array:
$newarray = {value_01,value_11,value_n1......value_nn}
Upvotes: 0
Views: 64
Reputation: 2003
// Loop through your original array
foreach($myarray as $array){
// ensures that the keys are in alphabetical/numerical order
ksort($array);
// add the first value into your new array
$newarray[] = reset($array);
}
Upvotes: 0
Reputation: 1769
You can use array_map
to get what you want on each item of your array :
$array = array_map(function ($item) { return current($item); }, $array);
In your case, this seems to be the first value of the item, so you use current
.
Reference : https://www.php.net/manual/function.array-map.php
Upvotes: 0
Reputation: 79024
You can get the first value of each sub-array with current
or reset
:
$newarray = array_map('current', $array);
If you need a certain offset (in this case the first) then get the values from each array re-indexed with integers and then extract column 0:
$newarray = array_column(array_map('array_values', $array), 0);
Upvotes: 1