I am trying a solution without going through "manually" ( foreach
) an array, to add in the subarrays, a new pair of chave => valor
. With array_walk
I can invoke a callback and treat the array but how do I pass a variable to its callback?
The array has the following structure:
$array = [
[
'id' => 1,
'name' => 'abc'
],
[
'id' => 2,
'name' => 'def'
]
];
I tried to add the new pairs as follows:
array_walk($array, function ($v, $k) { $v['token'] = $token; });
But I get a% w / w of% even with it declared and assigned value.
Using the key Undefined variable
in the use
function, it does not add the new key:
function ($v, $k) use ($token) { $v['token'] = $token; }