-5

Is there any array function by which I can get the expected result?

My array

$a = array(
[0] => array( 'id' => 6 ),
[1] => array( 'id' => 5 ),
[2] => array( 'id' => 8 ),
[3] => array( 'id' => 4 ),
);

Result Expected

$a = array(
[0] => 6,
[1] => 5 ,
[2] => 8 ,
[3] => 4,
);

I can do that using foreach loop. But I am searching for a array function ..

Goutam Pal
  • 1,763
  • 1
  • 10
  • 14

4 Answers4

1

Strictly, yes, there is a function array_walk():

array_walk($a, function (&$value) {
    $value = $value['id'];
});

But a foreach loop is probably more efficient in this case:

foreach ($a as &$value) {
    $value = $value['id'];
}

A foreach loop has very little overheads compared to an array_walk, which has to create and destroy a function call stack on each invokation of the callback function.

Note that in each case, $value is passed by reference (using the & operator). This means that the array is changed in place, no copying of the array is necessary.

Luke Mills
  • 1,616
  • 1
  • 11
  • 18
  • @DevlshOne, I didn't see you're comment until after I posted my answer, I promise :) – Luke Mills Jul 11 '13 at 06:50
  • Note that the array is never *copied* as such, even without passing by reference. PHP is more intelligent than to needlessly duplicate data. – deceze Jul 11 '13 at 06:56
1

This will give the expected output in $a

$a = array_map('current', $a);

Reference

Gordon
  • 312,688
  • 75
  • 539
  • 559
deceze
  • 510,633
  • 85
  • 743
  • 889
  • @tere Seriously?! Answering a question is now repwhoring? Especially when the rest of the answers are dominated by IMO not so great answers? There's simply nothing else to say about this one liner. – deceze Jul 11 '13 at 07:03
  • @deceze How would one go about repwhoring without posting answers, that's my question to you. – Manuel Jul 11 '13 at 07:07
  • 2
    Just as a side note: While I agree that the question is poor and bears the question why the OP needs a different solution, no one provided a dupe or closevote, so I guess answering this is fine. However, a one liner is almost *never* a good answer and 20k+ users should be role models when it comes to answering. Adding a link to the manual and adding a small explanation doesn't take much effort. – Gordon Jul 11 '13 at 07:16
  • @Gordon Hint taken. Much more eloquently expressed. :) But seriously, sometimes I have a hard time saying anything more about self explanatory one liners... – deceze Jul 11 '13 at 07:18
0

Foreach seems more efficient in the situation.

$resultArray = array();
foreach ($a as $key => $value){
    $resultArray[] = $value['id'];
}
Duikboot
  • 1,093
  • 1
  • 12
  • 24
-1

Here is how you can use array_walk,

$a = array(
0 => array( 'id' => 6 ),
1 => array( 'id' => 5 ),
2 => array( 'id' => 8 ),
3 => array( 'id' => 4 ),
);


array_walk($a, function ($ar, $val) {
       global $a;
       $a[$val] = $ar['id'];
    }
);

The resulting array will produce

(
    [0] => 6
    [1] => 5
    [2] => 8
    [3] => 4 )

For php versions 5.5 and up the following command should work.

$a = array_column($a, 'id');
DevZer0
  • 13,433
  • 7
  • 27
  • 51