Search and replace inside an associative array

后端 未结 10 1933
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 22:14

I need to search and replace inside an associative array.

ex:

$user = \"user1\"; // I\'ve updated this

$myarray = array(\"user1\" => \"search1\",         


        
10条回答
  •  自闭症患者
    2021-01-20 22:31

    There's a function for this : array_map().

    // Using a lamba function, PHP 5.3 required
    $newarray = array_map(
        function($v) { if ($v == 'search1') $v = 'search4'; return $v; },
        $myarray
    );
    

    If you don't want to use a lambda function, define a normal function or method and callback to it.

提交回复
热议问题