Home » Php » search and replace value in PHP array

search and replace value in PHP array

Posted by: admin April 23, 2020 Leave a comment


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.

How to&Answers:

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_keys($ar, $value),


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.


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;



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];


What about array_walk() with callback?

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


Depending whether it’s the value, the key or both you want to find and replace on you could do something like this:

$array = json_decode( str_replace( $replace, $with, json_encode( $array ) ), true );

I’m not saying this is the most efficient or elegant, but nice and simple.


$ar[array_search('green', $ar)] = 'value';


Based on Deept Raghav‘s answer, I created the follow solution that does regular expression search.

$arr = [
    'Array Element 1',
    'Array Element 2',
    'Replace Me',
    'Array Element 4',

$arr = array_replace(
            preg_grep('/^Replace/', $arr)
        'Array Element 3'

echo '<pre>', var_export($arr), '</pre>';

PhpFiddle: http://phpfiddle.org/lite/code/un7u-j1pt


 <b>$green_key = array_search('green', $input); // returns the first key whose value is 'green'

$input[$green_key] = ‘apple’; // replace ‘green’ with ‘apple’