Php array add key value pair to existing array
Im trying to add a key=>value to a existing array with a specific value. Show Im basically looping through a associative array and i want to add a key=>value foreach array that has a specific id: ex:
I want to do it so that while
so $some_value is going to be associated with the specific id.
Joseph Silber 208k57 gold badges356 silver badges288 bronze badges asked Jul 12, 2012 at 22:40
0 The while loop doesn't make sense since keys are unique in an associative array. Also, are you sure you want to modify the array while you are looping through it? That may cause problems. Try this:
A more efficient way is this:
or if its a key in the array you want to match and not the value...
answered Jul 12, 2012 at 22:46
TuckerTucker 6,8079 gold badges36 silver badges55 bronze badges 1 When you use:
... the $arr variable is a local copy that is only scoped to that foreach. Anything you add to it will not affect the $array variable. However, if you call $arr by reference:
... now if you add a new key to that array it will affect the $array through which you are looping. I hope I understood your question correctly. answered Jun 24, 2013 at 20:59
If i understood you correctly, this will be the solution:
answered Jul 12, 2012 at 22:46
BesnikBesnik 6,3031 gold badge30 silver badges32 bronze badges (PHP 4, PHP 5, PHP 7, PHP 8) array_push — Push one or more elements onto the end of array Descriptionarray_push(array repeated for each passed value.
Parametersarray The input array. values The values to push onto the end of the Return ValuesReturns the new number of elements in the array. Changelog
ExamplesExample #1 array_push() example
The above example will output: Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry ) See Also
Rodrigo de Aquino ¶ 10 years ago
bxi at apparoat dot nl ¶ 14 years ago
mrgreen dot webpost at gmail dot com ¶ 6 years ago
raat1979 at gmail dot com ¶ 6 years ago
"\npush 1 returns ".array_push($pr,1)."\n"; willdemaine at gmail dot com ¶ 14 years ago
timer() yhusky at qq dot com ¶ 4 years ago
egingell at sisna dot com ¶ 16 years ago
= array('a', 'b', 'c'); asma dot gi dot 14 at gmail dot com ¶ 10 months ago
David Spector ¶ 1 year ago
P.A.Semi ¶ 3 years ago
Carlos Alberto B. Carucce ¶ 3 years ago
aosojnik at gmail dot com ¶ 12 years ago
gfuente at garrahan dot gov dot ar ¶ 5 years ago
helpmepro1 at gmail dot com ¶ 13 years ago
aaron dot hawley at uvm dot edu ¶ 17 years ago
flobee ¶ 8 years ago
Phil Davies ¶ 17 years ago
andrew at cgipro dot com ¶ 17 years ago
bk at quicknet dot nl ¶ 17 years ago
steve at webthoughts d\ot ca ¶ 16 years ago
David Spector ¶ 1 year ago
siqueiramoises14 at gmail dot com ¶ 2 years ago
wesleys at opperschaap dot net ¶ 13 years ago
is_array($arr)) { colecooper2005 at icloud dot com ¶ 5 years ago
golddragon007 ¶ 7 years ago
oneill at c dot dk ¶ 17 years ago
Chicna ¶ 10 years ago
alexander dot williamson at gmail dot com ¶ 14 years ago
zbde00 at hotmail dot com ¶ 15 years ago
yuri ¶ 10 years ago
Marc Bernet ¶ 16 years ago
john ¶ 16 years ago
rarioj at gmail dot com ¶ 12 years ago
kamprettos at yahoo dot com Teguh Iskanto ¶ 17 years ago
ciprian dot amariei at gmail com ¶ 17 years ago
bart at framers dot nl ¶ 20 years ago
How do I add a key to an existing array?If you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following: $data[$key] = $value; It is not necessary to use array_push.
How do you add a key value pair to an array?To add a key/value pair to all objects in an array:
Use the Array. forEach() method to iterate over the array. On each iteration, use dot notation to add a key/value pair to the current object. The key/value pair will get added to all objects in the array.
How do you create an array of key value pairs in PHP?Creating a Numeric Array
If you create an array with [] or array() by specifying only a list of values instead of key/value pairs, the PHP engine automatically assigns a numeric key to each value. The keys start at 0 and increase by one for each element.
What is Array_keys () used for in PHP?The array_keys() function returns an array containing the keys.
|