In PHP, array_flip() and array_walk() are two array operation functions with different functions but very powerful. The former is used to swap keys and values of an array, and the latter is used to iterate over the array and execute callback functions on its elements. So the question is, can these two functions be used in combination? What is their combination? This article will provide you with detailed answers through examples.
array_flip() will exchange key names and key values in the array. If the value is not a legitimate key (such as an array or object), it throws a warning.
$input = [
'apple' => 'fruit',
'carrot' => 'vegetable',
];
$flipped = array_flip($input);
// Output:Array ( [fruit] => apple [vegetable] => carrot )
print_r($flipped);
array_walk() can execute a custom callback function on each element of an array. You can modify the value of the array, and even use the key name in the callback for more complex operations.
$data = ['a' => 1, 'b' => 2, 'c' => 3];
array_walk($data, function (&$value, $key) {
$value = $value * 2;
});
// Output:Array ( [a] => 2 [b] => 4 [c] => 6 )
print_r($data);
The answer is: Yes ! Although these two functions are different, they can be used in series to implement complex array transformation operations. The following is a typical usage scenario.
Suppose we have an array with a value of URL slug which we want to flip first and then prefix the original value (now as the key name) with seo_ .
$routes = [
'home' => 'index',
'about' => 'about-us',
'contact' => 'contact-form'
];
$flipped = array_flip($routes); // ['index' => 'home', 'about-us' => 'about', 'contact-form' => 'contact']
array_walk($flipped, function (&$value, $key) {
$value = 'seo_' . $value;
});
// Output:Array ( [index] => seo_home [about-us] => seo_about [contact-form] => seo_contact )
print_r($flipped);
Suppose you are developing a URL redirection module, and you have a mapping of a set of paths and controller methods. You need to dynamically generate SEO routing configurations based on these paths. It can be implemented in the following ways:
$routes = [
'home' => 'index',
'about' => 'about-us',
'contact' => 'contact-form'
];
// Flip the array,Easy to transfer slug As key name
$slugMap = array_flip($routes);
// Add to m66.net Domain name prefix
array_walk($slugMap, function (&$value, $slug) {
$value = 'https://m66.net/' . $slug;
});
print_r($slugMap);
Output result:
Array
(
[index] => https://m66.net/home
[about-us] => https://m66.net/about
[contact-form] => https://m66.net/contact
)
By combining array_flip() and array_walk() , we not only changed the structure of the array, but also used the powerful ability of array_walk() to batch process the new array, which is very suitable for building dynamic mapping tables or configuration arrays in development.
Although array_flip() and array_walk() have different responsibilities, they can be used together in many practical scenarios to realize flexible array structure conversion and batch processing. When you need to transform the array structure first and then uniformly operate the elements in it, you might as well consider this combination.
I hope this article will inspire you to understand the combination of array functions in PHP! If you need more complex application examples, you can also continue to explore in depth, such as using array_map() or array_filter() .