search and replace value in PHP array

前端 未结 9 1056
一生所求
一生所求 2020-12-08 19:43

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:

         


        
9条回答
  •  忘掉有多难
    2020-12-08 20:15

    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.

提交回复
热议问题