Home > Backend Development > PHP Problem > How to replace specified elements of array with php string

How to replace specified elements of array with php string

PHPz
Release: 2023-04-27 14:34:12
Original
589 people have browsed it

In PHP, string replacement and array operations are very common and practical functions. Sometimes we need to replace certain contents in a string according to certain rules, or operate on specific elements in an array. This article will introduce the related functions of string replacement and array operations in PHP, and explain them with examples.

String replacement

There are many functions for string replacement in PHP, including str_replace(), preg_replace(), etc. Among them, the str_replace() function is the most commonly used string replacement function. The basic syntax is as follows:

str_replace($search, $replace, $subject);
Copy after login

Among them, $search represents the string that needs to be found and replaced, $replace represents the string used to replace, and $subject represents the original string being replaced.

The following is a simple sample code:

<?php
 $str = "Hello, world!";
 $new_str = str_replace("world", "PHP", $str);
 echo $new_str;
?>
Copy after login

The running result is: Hello, PHP!

In practical applications, we sometimes need to compare multiple characters in a string Content is replaced. At this time we can use arrays to implement multiple sets of replacements. The specific operation is as follows:

<?php
  $str = "Hello, Alex!";
  $search_arr = array("Hello", "Alex");
  $replace_arr = array("Hi", "Peter");
  $new_str = str_replace($search_arr, $replace_arr, $str);
  echo $new_str;
?>
Copy after login

The operation result is: Hi, Peter!

It should be noted that the string replacement function in PHP is not case-sensitive, that is, if the variable contains String case does not match $search and will still be replaced. If you need to perform case-sensitive replacement, you can use the str_ireplace() function. Its usage is basically the same as str_replace().

Array operation

PHP array operation is very convenient and practical. PHP's built-in array functions provide many functions for operating on arrays, including array_push(), array_pop(), etc. Below, we will focus on the two functions array_replace() and array_replace_recursive(), and how to obtain the value at the specified position in the array.

The array_replace() function is used to merge and replace two arrays and return a new array. The syntax is as follows:

 mixed array_replace ( array $array1 , array $array2 [, array $... ] )
Copy after login

Among them, $array1 represents the original array that needs to be replaced, $array2 represents the new array used for replacement, $... represents an optional additional array, used for more Array replacement operation.

For example, we have two arrays, $arr1 and $arr2, where $arr2 should replace the contents of $arr1. This can be achieved through the array_replace() function. The specific code is as follows:

<?php
   $arr1 = array("a" => "apple", "b" => "banana");
   $arr2 = array("a" => "orange", "c" => "coconut");
   $new_arr = array_replace($arr1, $arr2);
   print_r($new_arr);
?>
Copy after login

The execution result is:

Array ( [a] => orange [b] => banana [c] => coconut )
Copy after login

In the above code, the array_replace() function replaces the contents of array $arr2 to array$ arr1, so in the new array finally output, the value with key "a" becomes "orange".

If the same key name exists in $arr1 and $arr2, then in the array replaced using the array_replace() function, the value corresponding to the same key name will be replaced with the corresponding value in $arr2, not is covered. If you need a complete replacement, you can use the array_merge() function.

The array_replace_recursive() function is similar to the array_replace() function and is also used to merge and replace arrays. However, the array_replace_recursive() function supports merged replacement of multi-level nested arrays. The basic syntax is as follows:

 mixed array_replace_recursive ( array $array1 , array $array2 [, array $... ] )
Copy after login

Similar to the array_replace() function, $array1 and $array2 represent the original array and new array that need to be replaced respectively, $... represents an optional additional array.

The following is a sample code:

<?php
  $arr1 = array("a" => array("apple", "apricot"), "b" => "banana");
  $arr2 = array("a" => array("orange"), "c" => "coconut");
  $new_arr = array_replace_recursive($arr1, $arr2);
  print_r($new_arr);
?>
Copy after login

The execution result is:

Array ( [a] => Array ( [0] => orange [1] => apricot ) [b] => banana [c] => coconut )
Copy after login

In the above code, $arr1 and $arr2 contain two levels and one level of nesting respectively. array. After merging and replacing through the array_replace_recursive() function, the value corresponding to the $a key in the new array is replaced with the value in $arr2, while retaining the second element "apricot" in $arr1.

In addition to merging and replacing, obtaining the value at a specified position in the array is also a common requirement in PHP array operations. In PHP, you can use [] or {} to access the value at a specified position in the array. For example:

<?php
  $arr = array("apple", "banana", "watermelon");
  echo $arr[0];  // 输出"apple"
?>
Copy after login

[] is used here to access the first element in the array.

If you want to access the elements of a nested subarray in an array, you can use multiple [] or {}. For example:

<?php
  $arr = array("fruit" => array("a" => "apple", "b" => array("banana", "blueberry")));
  echo $arr["fruit"]["b"][1];  // 输出"blueberry"
?>
Copy after login

In the above code, the element corresponding to the $b key in the $arr array is a two-dimensional array, and its second element can be easily accessed through multiple [] or {}.

Conclusion

In actual development, string and array operations are very common and practical functions. Through the introduction and examples of this article, readers can better master the related functions of string replacement and array operations in PHP, and flexibly apply them to their own development work.

The above is the detailed content of How to replace specified elements of array with php string. For more information, please follow other related articles on the PHP Chinese website!

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