Replace string in an array with PHP

后端 未结 6 613
傲寒
傲寒 2020-12-09 17:57

How can I replace a sub string with some other string for all items of an array in PHP?

I don\'t want to use a loop to do it. Is there a predefined function in PHP t

6条回答
  •  天涯浪人
    2020-12-09 18:38

    With array_walk_recursive()

    function replace_array_recursive( string $needle, string $replace, array &$haystack ){
        array_walk_recursive($haystack,
            function (&$item, $key, $data){
            $item = str_replace( $data['needle'], $data['replace'], $item );
            return $item;
        },
            [ 'needle' => $needle, 'replace' => $replace ]
        );
    }
    

提交回复
热议问题