5
I’m trying a solution without going through "manually"(foreach
) an array, to add in the subarrays, a new pair of chave => valor
. With the array_walk
I can invoke a callback and treat the array but as 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 one Undefined variable
whether declared and with an assigned value.
Using the key use
in the role of callback
, it does not add the new key:
function ($v, $k) use ($token) { $v['token'] = $token; }