I want to add an element to every array in my multidimensional array.
My array is like this:
Array
(
[0] => Array
(
[RatingFactorPreferenceID] => 10
[PreferenceID] => 45
[RatedValue] => 1
[CreatedOn] => 1326779061
[CreatedBy] => 25
)
[1] => Array
(
[RatingFactorPreferenceID] => 20
[PreferenceID] => 45
[RatedValue] => 2
[CreatedOn] => 1326779061
[CreatedBy] => 25
)
)
I want to add [RatingID] => 2 to both arrays, then my final array would look like:
Array
(
[0] => Array
(
[RatingID] => 2
[RatingFactorPreferenceID] => 10
[PreferenceID] => 45
[RatedValue] => 1
[CreatedOn] => 1326779061
[CreatedBy] => 25
)
[1] => Array
(
[RatingID] => 2
[RatingFactorPreferenceID] => 20
[PreferenceID] => 45
[RatedValue] => 2
[CreatedOn] => 1326779061
[CreatedBy] => 25
)
)
I can loop over my array and do this, is there a better way to do this?
This still loops, but behind the scenes, if you prefer that.
You could also use
array_walk, but that’s really just looping disguised in a different syntax.