Home > Article > Backend Development > How to combine values with the same key in an array into one in php
In the process of PHP programming, it is often necessary to deal with the same key in the array. If there are elements with the same key in an array, how to merge them into one? The following is a brief introduction to some methods of processing arrays in PHP to help you solve this problem.
1. Use the array_merge_recursive() function
PHP’s built-in array_merge_recursive() function can merge two or more arrays into one array. If the same key appears in one of the arrays, the different sub-items will be merged into one array, and the final result will be a multi-dimensional array.
The sample code is as follows:
$arr1 = array("name"=>"张三","age"=>18,"hobbies"=>array("football","basketball")); $arr2 = array("name"=>"李四","age"=>20,"hobbies"=>array("swimming","shopping")); $result = array_merge_recursive($arr1,$arr2);
The result after execution is:
array( "name"=>array("张三","李四"), "age"=>array(18,20), "hobbies"=>array( array("football","basketball"), array("swimming","shopping") ) )
You can see that the same key("name","age","hobbies" ) values are merged and turned into a multidimensional array.
2. Use loop traversal
In addition to using the array_merge_recursive() function, you can also use loop traversal to merge elements with the same key in the array into one.
The sample code is as follows:
$arr = array( "name"=>"张三", "age"=>18, "grade"=>"一年级", "major"=>"计算机科学", "score"=>array("英语"=>80,"数学"=>90), "address"=>array("province"=>"广东省","city"=>"深圳市") ); $newArr = array(); foreach($arr as $key=>$value){ if(array_key_exists($key,$newArr)){ if(!is_array($newArr[$key])){ $tmp = $newArr[$key]; $newArr[$key] = array(); array_push($newArr[$key],$tmp); } array_push($newArr[$key],$value); }else{ $newArr[$key] = $value; } }
The result obtained after execution is:
array( "name"=>"张三", "age"=>18, "grade"=>"一年级", "major"=>"计算机科学", "score"=>array("英语"=>80,"数学"=>90), "address"=>array("province"=>"广东省","city"=>"深圳市") )
As you can see, there are no elements with the same key in the original array, so $newArr is different from the original array Exactly the same.
3. Use the array_reduce() function
In addition to the above two methods, you can also use the array_reduce() function that comes with PHP to merge elements with the same key in the array into one.
The sample code is as follows:
$arr = array( "name"=>"张三", "age"=>18, "grade"=>"一年级", "major"=>"计算机科学", "score"=>array("英语"=>80,"数学"=>90), "address"=>array("province"=>"广东省","city"=>"深圳市") ); $newArr = array_reduce(array_keys($arr),function($result,$key) use ($arr){ if(array_key_exists($key,$result)){ if(!is_array($result[$key])){ $tmp = $result[$key]; $result[$key] = array(); array_push($result[$key],$tmp); } array_push($result[$key],$arr[$key]); }else{ $result[$key] = $arr[$key]; } return $result; },array()); print_r($newArr);
The result after execution is the same as the result of method 2 above.
To sum up, there are many ways to process elements with the same key in an array in PHP, and each method has its applicable situations. You can choose the most appropriate method to deal with it according to the actual situation.
The above is the detailed content of How to combine values with the same key in an array into one in php. For more information, please follow other related articles on the PHP Chinese website!