Replace one value in array php
I was looking for some standard PHP function to replace some value of an array with other, but surprisingly I haven't found any, so I have to write my own: Show
But I still wonder - for such an easy thing there must already be some function for it! Or maybe much easier solution than this one invented by me? Note that this function will only do one replacement. I'm looking for existing solutions that similarly replace a single occurrence, as well as those that replace all occurrences.
outis 73.2k19 gold badges146 silver badges216 bronze badges asked Dec 29, 2011 at 14:05
12 Instead of
a function that only replaces occurrences of one value in an array, there's the more general
To replace multiple occurrences of multiple values using array of value => replacement:
To replace a single occurrence of one value, you'd use answered Dec 29, 2011 at 14:19
outisoutis 73.2k19 gold badges146 silver badges216 bronze badges 5 While there isn't one function equivalent to the sample code, you can use EDIT by Tomas: the code was not working, corrected it:
Tomas 55.6k48 gold badges230 silver badges364 bronze badges answered Dec 29, 2011 at 14:09
Deept RaghavDeept Raghav 1,41913 silver badges14 bronze badges 5 If performance is an issue, one may consider not to create multiple functions within
rmorse 7166 silver badges17 bronze badges answered Sep 1, 2015 at 0:12
BurninLeoBurninLeo 4,0503 gold badges36 silver badges52 bronze badges Try this function.
Cheers!
Joshua 4,5592 gold badges28 silver badges43 bronze badges answered Dec 29, 2011 at 14:13
Prasad RajapakshaPrasad Rajapaksha 5,99410 gold badges35 silver badges51 bronze badges 0
answered Mar 11, 2019 at 14:26
2
Depending whether it's the value, the key or both you want to find and replace on you could do something like this:
I'm not saying this is the most efficient or elegant, but nice and simple. answered Sep 5, 2016 at 19:50
What about
answered Sep 13, 2016 at 14:27
user3396065user3396065 5205 silver badges15 bronze badges Based on
PhpFiddle: http://phpfiddle.org/lite/code/un7u-j1pt answered Mar 4, 2016 at 22:45
PHP 8, a strict version to replace one string value into another:
An example - replace value
answered Aug 9, 2021 at 20:38
1
$input[$green_key] = 'apple'; // replace 'green' with 'apple' answered Dec 29, 2011 at 14:17
Muthu KrishnanMuthu Krishnan 1,6662 gold badges10 silver badges15 bronze badges 1 How do you replace values in an array?To replace an element in an array:
Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.
How to edit array value in PHP?In some scenarios, you may need or be required to update the values of multiple array elements all at once. To do that, you will need to use the array_replace() function. The array_replace() is an in-built PHP function that replaces the values of an array with values of another array having the same keys or indices.
How to replace array keys in PHP?function replace_key($arr, $oldkey, $newkey) {. if(array_key_exists( $oldkey, $arr)) {. $keys = array_keys($arr);. $keys[array_search($oldkey, $keys)] = $newkey;. return array_combine($keys, $arr);. How do I change the index of an array element in PHP?We will use array_values() function to get all the values of the array and range() function to create an array of elements which we want to use as new keys or new index of the array (reindexing). Then the array_combine() function will combine both the array as keys and values.
|