How to replace specified value in array in php? Brief analysis of three methods

PHPz
Release: 2023-04-11 13:42:02
Original
2548 people have browsed it

In PHP programming, replacing the specified value in an array is a common operation. In this article, we will learn how to write code to replace specified values ​​in an array using PHP. We will introduce the following:

1. An example of using a for loop to traverse an array and replace values

2. An example of using the array_map() function to replace a specified value in an array

3. An example of using the array_walk() function to replace the specified value in the array

1. Using a for loop to replace the specified value in the array

Use for Looping through an array and replacing values ​​is a common approach. Below is a sample code that shows how to use a for loop to iterate through an array and replace values.

$myArray = array(1, 2, 3, 4, 5);

for ($i = 0; $i < count($myArray); $i++) {
    if ($myArray[$i] == 3) {
        $myArray[$i] = &#39;three&#39;;
    }
}

print_r($myArray);
Copy after login

In this example, our array is $myArray, and this array has five elements: 1, 2, 3, 4 and 5. We use a for loop to iterate through this array. For each element, we check if it is equal to 3. If equals 3, then we replace this element with the string "three", otherwise do nothing. Finally, we use the print_r() function to output the modified array. The output is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => three
    [3] => 4
    [4] => 5
)
Copy after login

As you can see, we have successfully replaced the number 3 in the array with the string "three".

2. Use the array_map() function to replace the specified value in the array

The array_map() function is a powerful function built into PHP that can The element performs a custom function operation. The following is a sample code that uses the array_map() function to replace a specified value in an array.

function replaceThree($num) {
  if ($num == 3) {
    return 'three';
  } else {
    return $num;
  }
}

$myArray = array(1, 2, 3, 4, 5);
$myArray = array_map('replaceThree', $myArray);
print_r($myArray);
Copy after login

In this example, we first define a function called replaceThree. This function accepts a parameter $num and checks if it is equal to 3. If so, return the string "three", otherwise return $num itself.

Then, we create an array named $myArray, which has five elements. We use the array_map() function to apply the replaceThree() function to each element of $myArray. The result is a new array, in which each element of $myArray is replaced with the string "three". The output result is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => three
    [3] => 4
    [4] => 5
)
Copy after login
Copy after login

3. Use the array_walk() function to replace the specified value in the array

Another built-in function in PHP, the array_walk() function, can be used to walk through an array and apply a custom function to it. The following is a sample code that uses the array_walk() function to replace a specified value in an array.

function replaceThree(&$value, $key) {
  if ($value == 3) {
    $value = 'three';
  }
}

$myArray = array(1, 2, 3, 4, 5);
array_walk($myArray, 'replaceThree');
print_r($myArray);
Copy after login

In this example, we define a function called replaceThree. Unlike the array_map() function, the first parameter of the replaceThree() function is $value, which is passed by reference, which means that any changes will affect the original array. We also define a second parameter $key, which is optional but needs to be declared in the function signature, in order to represent the key of the array element currently being processed.

Then, we create an array named $myArray, which has five elements. We use the array_walk() function to walk through $myArray and call the replaceThree() function on each element. If the value of the element is equal to 3, we replace the element with the string "three".

Finally, we use the print_r() function to display the modified $myArray array. The output results are as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => three
    [3] => 4
    [4] => 5
)
Copy after login
Copy after login

Conclusion

In PHP In programming, replacing a specified value in an array is a common operation. In this article, we learned three different ways to accomplish this task. Using a for loop to iterate through the array and replace values ​​is the most basic method, but using the array_map() function and array_walk() function can make the code more concise and easier to maintain. You can use these methods according to your specific needs to achieve the best results.

The above is the detailed content of How to replace specified value in array in php? Brief analysis of three methods. 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