search and replace value in PHP array

php replace value in multidimensional array
php change array value in foreach
php array map
php change array key
php str_replace(array)
array replace
php replace special characters
php str_replace multiple

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:

function array_replace_value(&$ar, $value, $replacement)
{
    if (($key = array_search($ar, $value)) !== FALSE) {
        $ar[$key] = $replacement;
    }
}

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.


While there isn't one function equivalent to the sample code, you can use array_keys (with the optional search value parameter), array_fill and array_replace to achieve the same thing:

EDIT by Tomas: the code was not working, corrected it:

$ar = array_replace($ar,
    array_fill_keys(
        array_keys($ar, $value),
        $replacement
    )
);

array_replace - Manual, If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject . If replace has fewer values� array_replace() replaces the values of array1 with values having the same keys in each of the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array.


Instead of a function that only replaces occurrences of one value in an array, there's the more general array_map:

array_map(function ($v) use ($value, $replacement) {
    return $v == $value ? $replacement : $v;
}, $arr);

To replace multiple occurrences of multiple values using array of value => replacement:

array_map(function ($v) use ($replacement) {
    return isset($replacement[$v]) ? $replacement[$v] : $v;
}, $arr);

To replace a single occurrence of one value, you'd use array_search as you do. Because the implementation is so short, there isn't much reason for the PHP developers to create a standard function to perform the task. Not to say that it doesn't make sense for you to create such a function, if you find yourself needing it often.

str_replace - Manual, replace keys $new_keys = str_replace('/attachment/', '/attach/', array_keys($ subject)); // 2. combine new keys with the original values $replaced =� The array_replace() function replaces the values of the first array with the values from following arrays. Tip: You can assign one array to the function, or as many as you like. If a key from array1 exists in array2, values from array1 will be replaced by the values from array2.


Try this function.

public function recursive_array_replace ($find, $replace, $array) {
    if (!is_array($array)) {
        return str_replace($find, $replace, $array);
    }

    $newArray = [];
    foreach ($array as $key => $value) {
        $newArray[$key] = recursive_array_replace($find, $replace, $value);
    }
    return $newArray;
}

Cheers!

Array search and replace - PHP, Specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones. Technical Details. Return Value:� Parameters. needle. The searched value. Note: . If needle is a string, the comparison is done in a case-sensitive manner.. haystack. The array. strict. If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the haystack.


If performance is an issue, one may consider not to create multiple functions within array_map(). Note that isset() is extremely fast, and this solutions does not call any other functions at all.

$replacements = array(
    'search1' => 'replace1',
    'search2' => 'replace2',
    'search3' => 'replace3'
);
foreach ($a as $key => $value) {
    if (isset($replacements[$value])) {
        $a[$key] = $replacements[$value];
    }
}

PHP array_replace() Function, If find is an array and replace is a string, the replace string will be used for every find value. Note: This function is case-sensitive. Use the str_ireplace() function to � Return Value: Returns the key of a value if it is found in the array, and FALSE otherwise. If the value is found in the array more than once, the first matching key is returned. PHP Version: 4.0.5+ PHP Changelog: This function returns NULL if invalid parameters are passed to it (this applies to all PHP functions as of 5.3.0).


What about array_walk() with callback?

$array = ['*pasta', 'cola', 'pizza'];
$search = '*';
$replace = '\*';
array_walk($array,
    function (&$v) use ($search, $replace){
        $v = str_replace($search, $replace, $v);    
    }                                                                     
);  
print_r($array);

PHP str_replace() Function, If search and replace are arrays, then str_replace() takes a value from each array and uses $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); Questions: want to replace the array value with 0, exclude work and home arra


Replace all occurrences of the search string with the replacement , Return Value: This function returns a string or an array based on the $subjectVal parameter Array containing replace string from search string. The array_search() function searches an array for a given value and returns the key. The function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned. Syntax array_search(val, arr, strict) Parameters


PHP, On this page we describe and demonstrate the following PHP functions for If the search argument is an array and the replace argument is a string, each Notice that the value passed as the subject argument is not enclosed in quotes. Today I needed to replace the value of certain key in array. I do not need to add another key and value. Rather, replace an existing key's value with another value. Since I am using an associative array and knew the key's name as well. I wrote the following code and it worked. PHP


Replacing Strings in PHP, Get code examples like "how to replace array key php" instantly right from your google function array_replace_key($search, $replace, array $subject) {. 2. $ updatedArray = [];. 3. . 4. foreach ($subject as $key => $value) {. 5. This post shows the possible ways to remove specific element from array based on value of the element. Removing specific value Using array_diff(). Using this method we can remove one or more elements where using other methods we can only remove/delete one element at a given instance.