Home > PHP Framework > Laravel > body text

Comprehensive analysis of ThinkPHP array replacement

PHPz
Release: 2023-04-21 14:08:11
Original
744 people have browsed it

Manipulating arrays is a common task when writing PHP programs. In ThinkPHP projects, it is often necessary to replace certain values ​​in the array, such as replacing all empty strings ('') in the array with null. This article will introduce you to various array replacement methods in ThinkPHP.

1. Ordinary array replacement

First, let’s take a look at the replacement method of ordinary arrays. Suppose we have an array $arr, which contains four elements:

$arr ​​= array(

'name' => '张三',
'age' => 18,
'email' => 'zhangsan@example.com',
'gender' => '男',
Copy after login
Copy after login

);

If we want to remove all null characters in the array To replace the string with null, you can use a foreach loop to traverse the array and replace each element. The code is as follows:

foreach ($arr as &$value) {

if ($value === '') {
    $value = null;
}
Copy after login
Copy after login

}

Among them, &$value represents a reference to the array element, so it can be modified directly original array. After running the above code, the value of $arr will become:

array(

'name' => '张三',
'age' => 18,
'email' => 'zhangsan@example.com',
'gender' => '男',
Copy after login
Copy after login

);

If you want to replace the value of the specified key name, you can do it in the loop Add conditional judgment, the code is as follows:

foreach ($arr as $key => &$value) {

if ($key === 'email' && $value === 'zhangsan@example.com') {
    $value = 'lisi@example.com';
}
Copy after login

}

In the above code, we target email The value of the key name is replaced, that is, 'zhangsan@example.com' is replaced with 'lisi@example.com'. If you run this code, the value of $arr will become:

array(

'name' => '张三',
'age' => 18,
'email' => 'lisi@example.com',
'gender' => '男',
Copy after login

);

But this method has a disadvantage. If the array is large, the efficiency of using loops to traverse and replace will be very low. So we need to find a more efficient replacement method.

2. Recursive array replacement

If we need to replace all empty strings in a multi-dimensional array with null, then using a loop is no longer sufficient. At this time, we can use the recursive method to traverse the entire array and recurse on each sub-array until the most basic element is found for replacement.

The code is as follows:

function array_replace_recursive_null($arr) {

foreach ($arr as $key => &$value) {
    if (is_array($value)) {
        $value = array_replace_recursive_null($value);
    } elseif ($value === '') {
        $value = null;
    }
}
return $arr;
Copy after login

}

In the above code, we first determine whether the current element is an array, If it is, the function array_replace_recursive_null() is called recursively for replacement. If not, it is judged whether the current element is an empty string, and if so, it is replaced with null.

Use this function to replace the array, the code is as follows:

$arr ​​= array(

'name' => '张三',
'age' => 18,
'contact' => array(
    'email' => 'zhangsan@example.com',
    'phone' => '',
    'address' => array(
        'province' => '广东省',
        'city' => '',
        'district' => '番禺区',
    ),
),
Copy after login
Copy after login

);
$arr ​​= array_replace_recursive_null($arr);

If you run the above code, the value of $arr will become:

array(

'name' => '张三',
'age' => 18,
'contact' => array(
    'email' => 'zhangsan@example.com',
    'phone' => null,
    'address' => array(
        'province' => '广东省',
        'city' => null,
        'district' => '番禺区',
    ),
),
Copy after login
Copy after login

);

Recursive array replacement applies to the entire multi-dimensional array Replacement, but replacement of a single value still requires a loop traversal.

3. Use the array_map() function to replace the array_map() function in PHP. You can apply the specified callback function to each element in the array and return a new array. We can use this function to replace values ​​in an array.

Suppose we have an array containing the following data:

$arr ​​= array('apple', 'orange', 'banana', '');

We If you want to replace the empty string with null, you can use the array_map() function and a callback function.

The code is as follows:

function replace_null($value) {

return $value === '' ? null : $value;
Copy after login

}

$arr ​​= array_map('replace_null', $arr);


In the code, we wrote a replace_null() callback function. If $value is an empty string, it returns null, otherwise it returns $value. This function is then passed as a callback function to the array_map() function to complete the replacement of values ​​in the array.

4. Use array_walk_recursive() function to replace

array_walk_recursive() function is used to recursively apply user-defined functions to each element of an array or object. Similar to array_map(), but can recursively traverse multi-dimensional arrays. We can use this function to replace all values ​​in a multidimensional array.

Suppose we have a multidimensional array $arr, which contains some empty strings:

$arr ​​= array(

'name' => '张三',
'age' => 18,
'contact' => array(
    'email' => 'zhangsan@example.com',
    'phone' => '',
    'address' => array(
        'province' => '广东省',
        'city' => '',
        'district' => '番禺区',
    ),
),
Copy after login
Copy after login

);

Now , we need to replace all empty strings in it with null. This can be achieved using the array_walk_recursive() function and a callback function.

The code is as follows:

function replace_null_recursive(&$value, $key) {

if ($value === '') {
    $value = null;
}
Copy after login
Copy after login

}

array_walk_recursive($arr, 'replace_null_recursive');


In the code, we wrote a replace_null_recursive() callback function. If $value is an empty string, replace it with null, and then pass the function as a callback function to the array_walk_recursive() function.

After running the above code, the value of $arr will be as follows:

array(

'name' => '张三',
'age' => 18,
'contact' => array(
    'email' => 'zhangsan@example.com',
    'phone' => null,
    'address' => array(
        'province' => '广东省',
        'city' => null,
        'district' => '番禺区',
    ),
),
Copy after login
Copy after login

);

5. Summary

This article introduces various methods of replacing arrays in ThinkPHP projects, including ordinary array replacement, recursive array replacement, using array_map() function replacement and using array_walk_recursive() function replacement. Each method has applicable scenarios, and choosing the most appropriate method based on the actual situation can save time and energy.

Finally, I hope this article can be helpful to readers and help them complete the task more efficiently when dealing with array replacement.

The above is the detailed content of Comprehensive analysis of ThinkPHP array replacement. 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