I have an array that looks something like that
array(
[0] => array(
'id' => 1,
'title' => 'title 1',
),
[1] => array(
'id' => 10,
'title' => 'title 10',
),
[2] => array(
'id' => 11,
'title' => 'title 11',
),
[...]
);
I want to add an element to all the sub array. It’s the same element i’m adding. so the new array will look like :
array(
[0] => array(
'id' => 1,
'title' => 'title 1',
'type' => 'bag',
),
[1] => array(
'id' => 10,
'title' => 'title 10',
'type' => 'bag',
),
[2] => array(
'id' => 11,
'title' => 'title 11',
'type' => 'bag',
),
[...]
);
Is there a way to do it without iterating on the the first array?
It’s gonna be a big array. I’m looking for the fastest way to do it.
Whatever speed one might hope to gain by using array_walk, is lost with function overhead. Since you stated in your comments that the array is a result of a db query, you can simply include the
bagvalue in your result set by addingSELECT 'bag' AS 'type'to your SQL statement.