easy question but i can't find a solution.
I have an array:
array
0 =>
array
1 => string '25' (length=2)
1 =>
array
1 => string '27' (length=2)
And i need to get:
array
0 => string '25' (length=2)
1 => string '27' (length=2)
Yea if course i could do:
foreach($result as $each) {
$resultIds[]=$each[1];
}
But i am pretty sure i saw this week somewhere a function for it something like...
array_something('current',$result);
What will loop over the array as foreach and return the first element so the result will be the same as the foreach solution. But i can't find it or remember it.
*What is the name of the function ? *
You can use array_map
or array_walk
<?php
function first($n)
{
return $n[1];
}
$arr = array(
array(1, 2, 4),
array(1, 2, 3,),
);
var_export($arr);
// call internal function
$arr = array_map('current', $arr);
var_export($arr);
// call user function
$arr = array_map('first', $arr);
Please read manual here
$resultIds = array_map(function($arrEle) {
return $arrEle[0];
}, $result);
array_map takes a function which gets passed each of the elements of the array in turn, it is called for each array element.
The function then needs to return whatever you want in the new array, in this case you are wanting the second element of the child array so we return $arrEle[1]
;
after the whole array has been iterated over it returns the new array.