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

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.

Leave a Comment