Home >Backend Development >PHP Problem >How to replace the middle value (value) of an array in php

How to replace the middle value (value) of an array in php

PHPz
PHPzOriginal
2023-04-20 10:14:15559browse

PHP Replace value in array: use array_map and str_replace functions

In PHP, array is a very common data type and one of the data structures often used in our development process. When dealing with arrays, sometimes we need to replace certain element values ​​in the array to meet different business needs. This article will introduce how to use the array_map and str_replace functions in PHP to complete the operation of replacing value in an array.

Step 1. Create an array

First, we create an array to simulate a real application scenario. The code is as follows:

$topics = array(
  'PHP',
  'Laravel',
  'JavaScript',
  'Vue.js',
  'React',
  'Node.js'
);

The above code creates a $topics array, containing 6 elements, namely: PHP, Laravel, JavaScript, Vue.js, React, Node.js.

Step 2. Use the array_map function

In PHP, the array_map function can execute the specified callback function for each element in the array and return a new array. Therefore, we can use this function to traverse the original array and replace the value of the element that needs to be replaced with the new value. The following is a sample code for substitution using the array_map function:

$new_topics = array_map(function($topic) {
    if ($topic === 'Vue.js') {
        return 'Angular';
    } else {
        return $topic;
    }
}, $topics);

In the above code, we create an anonymous function and pass it as a parameter to the array_map function. The function of this function is: if the value of the current element is equal to "Vue.js", then return "Angular", otherwise return the original value. Finally, pass the original array $topics and the anonymous function to the array_map function to get a new array $new_topics.

Now, we output the $new_topics array and view the replacement result:

print_r($new_topics);

The output result is as follows:

Array
(
    [0] => PHP
    [1] => Laravel
    [2] => JavaScript
    [3] => Angular
    [4] => React
    [5] => Node.js
)

As can be seen from the output result, "Vue. js" element has been replaced with "Angular", and the values ​​of other elements remain unchanged.

Step 3. Use str_replace function

In addition to using the array_map function, we can also use the str_replace function in PHP to replace one or more element values ​​in the array. The following is a sample code that uses the str_replace function to replace a single element in an array:

$new_topics = $topics;
$key = array_search('Vue.js', $new_topics);
if ($key !== false) {
    $new_topics[$key] = 'Angular';
}

print_r($new_topics);

In the above code, we search the $topics array for an element with the value "Vue.js", and if found, replace it with "Angular" and assign the modified array to the $new_topics variable. Finally, we output the $new_topics array to see the replacement results.

The output result is as follows:

Array
(
    [0] => PHP
    [1] => Laravel
    [2] => JavaScript
    [3] => Angular
    [4] => React
    [5] => Node.js
)

As you can see from the output result, the "Vue.js" element in the original array has been replaced by "Angular", and the values ​​of other elements remain unchanged. Change.

In addition to replacing a single element, the str_replace function can also replace multiple element values ​​at the same time. The usage is also very simple, so I won’t go into details here.

Conclusion

Through the introduction of this article, we can find that it is very easy to replace array elements using the array_map function and str_replace function in PHP. For different scenarios, we can choose different solutions to achieve the best results. At the same time, in actual development, we should combine specific business needs and rationally use different technical means to improve code quality and development efficiency.

The above is the detailed content of How to replace the middle value (value) of an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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