Detailed explanation of the implementation method of using str_replace to replace multi-dimensional arrays in PHP

怪我咯
Release: 2023-03-12 17:56:01
Original
1507 people have browsed it

This article mainly introduces the implementation method of PHP using str_replace to replace multi-dimensional array, and combines specific examples to compare and analyze PHP's traversal of multi-dimensional array and Replacement operation related implementation skills and Notes, friends in need can refer to the following

This article describes the implementation method of using str_replace to replace multi-dimensional arrays in PHP. Share it with everyone for your reference, the details are as follows:

In php, if you use str_replace to replace the string in the array, you can only replace the one-dimensional array. If it is a multi-dimensional array, It is not possible to replace all strings. So I wrote a method to realize the replacement of multi-dimensional array strings.

First, let’s test using str_replace directly to test the replacement of the array.

$a = array(
'a' => 'bdc#',
'ceshi' => array('c'=>array('f'=>'#'))
);
$a = str_replace('#', 'ccc', $a);
var_dump($a);
Copy after login

I found that the output is as follows:

array (size=2)
 'a' => string 'bdcccc' (length=6)
 'ceshi' =>
  array (size=1)
   'c' =>
    array (size=1)
     'f' => string '#' (length=1)
Copy after login

It was found that only the strings in one-dimensional array were replaced, and arrays with more dimensions were not replaced.

Function to implement string replacement of multi-dimensional arrays

$a = array(
'a' => 'bdc#',
'ceshi' => array('c'=>array('f'=>'#'))
);
function strReplace(&$array) {
 $array = str_replace('#', 'ccc', $array);
if (is_array($array)) {
  foreach ($array as $key => $val) {
    if (is_array($val)) {
      strReplace($array[$key]);
    }
  }
}
}
strReplace($a);
var_dump($a);
Copy after login

The output is as follows:

array (size=2)
 'a' => string 'bdcccc' (length=6)
 'ceshi' =>
  array (size=1)
   'c' =>
    array (size=1)
     'f' => string 'ccc' (length=3)
Copy after login

It is found that string replacement of multi-dimensional arrays is implemented

The function strReplace is passed in The address of the array is used to replace the string recursively. Of course, this is just a very simple method, there are many ways to implement it, just like it!

The above is the detailed content of Detailed explanation of the implementation method of using str_replace to replace multi-dimensional arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!