In PHP, array_combine() and array_reduce() are very powerful functions, respectively used to combine two arrays into an associative array, and to reduce array elements according to specific rules (i.e. simplify or convert). When these two functions are used together, more complex data structures can be easily constructed. In this article, we will explore how to use these two functions in combination to build multi-layer nested arrays or other complex data structures.
array_combine() is a PHP built-in function that accepts two arrays as parameters, takes the value of the first array as the key, and the value of the second array as the value, and finally returns a new associative array. If the two arrays have different lengths, the function returns false .
$keys = ["name", "age", "gender"];
$values = ["John", 25, "male"];
$result = array_combine($keys, $values);
print_r($result);
Output:
Array
(
[name] => John
[age] => 25
[gender] => male
)
array_reduce() is also a PHP built-in function, which is used to gradually simplify the value of an array into a single value through a callback function. Typically, this value can be an accumulated sum or other complex structures.
$numbers = [1, 2, 3, 4, 5];
$sum = array_reduce($numbers, function($carry, $item) {
return $carry + $item;
});
echo $sum; // Output 15
array_combine() and array_reduce() are very useful when dealing with complex data structures, especially when you need to generate some sort of summary structure based on an array of key-value pairs. We can create a more complex, nested array by combining these two functions.
Suppose we have a set of data containing the student's grades, which we want to group by subject and then generate a summary of the grades for each subject through array_combine() .
$students = [
["name" => "John", "subject" => "Math", "score" => 85],
["name" => "Alice", "subject" => "Math", "score" => 92],
["name" => "Bob", "subject" => "Math", "score" => 78],
["name" => "John", "subject" => "Science", "score" => 88],
["name" => "Alice", "subject" => "Science", "score" => 94],
["name" => "Bob", "subject" => "Science", "score" => 79],
];
$subjects = array_reduce($students, function($carry, $item) {
// use subject As a key,Scores are grouped as values
if (!isset($carry[$item['subject']])) {
$carry[$item['subject']] = [];
}
$carry[$item['subject']][] = $item['score'];
return $carry;
}, []);
print_r($subjects);
Output:
Array
(
[Math] => Array
(
[0] => 85
[1] => 92
[2] => 78
)
[Science] => Array
(
[0] => 88
[1] => 94
[2] => 79
)
)
In this example, we first use array_reduce() to group students' scores based on subjects . Then we can further use array_combine() to map the student names and grades of each subject to build a more complex structure.
Next, we continue to use array_combine() to further break down the student scores in each subject by student name:
$students = [
["name" => "John", "subject" => "Math", "score" => 85],
["name" => "Alice", "subject" => "Math", "score" => 92],
["name" => "Bob", "subject" => "Math", "score" => 78],
["name" => "John", "subject" => "Science", "score" => 88],
["name" => "Alice", "subject" => "Science", "score" => 94],
["name" => "Bob", "subject" => "Science", "score" => 79],
];
$subjects = array_reduce($students, function($carry, $item) {
if (!isset($carry[$item['subject']])) {
$carry[$item['subject']] = [];
}
// 以学生的姓名As a key,Grades are mapped as values
$carry[$item['subject']] = array_combine(
array_column($carry[$item['subject']], 'name'),
array_column($carry[$item['subject']], 'score')
);
return $carry;
}, []);
print_r($subjects);
At this time, we extract the corresponding keys and values through array_column() , and finally we can build a more complex nested array, allowing us to search and operate according to subject and student names.
In this way, by combining array_combine() and array_reduce() , we can easily build data structures grouped and summarized according to different rules. In actual development, the flexibly using these functions can effectively improve the efficiency and readability of data processing.