当前位置:网站首页>In PHP, use recursive depth to merge multiple arrays
In PHP, use recursive depth to merge multiple arrays
2022-06-24 02:23:00 【lascyb】
Function source code :
// Import the array to be merged , quote $array Array receive
function merge(array &$array,array ...$mergeArray): array
{
foreach ($mergeArray as $item){
mergeOne($array,$item); // Execute the merge function for each array to be merged
}
return $array;
}
// If only two arrays need to be merged , You can also use this function directly
function mergeOne(&$array,$pushArray)
{
foreach ($pushArray as $key=>$item){ // Cycle through key values
if (is_array($item)){ // If the elements to be merged are also arrays , Make a deep merger
if(isset($array[$key])&&is_array($array[$key])){ // If the element corresponding to the original array and the key name is also an array
mergeOne($array[$key],$item); // Recursive deep merge
}else{// If the element corresponding to the key name of the original array is not an array , Directly covered
$array[$key]=$item;
}
}else{ // If the element to be merged is not an array , Assign directly by key name
$array[$key]=$item;
}
}
}Examples of use :
$a=[
"a"=>10,
"b"=>[
"b.a"=>10
],
"c"=>[
"c.a"=>50,
"c.b"=>60,
]
];
$b=[
"a"=>20,
"b"=>30,
"c"=>[
"c.a"=>40
]
];
$c=[
"a"=>30,
"b"=>[
"b.a"=>40
],
"c"=>[
"c.a"=>50,
"c.c"=>100
]
];
// Merge multiple arrays
merge($a,$b,$c);
var_dump($a);
// Merge two arrays
mergeOne($a,$b);
var_dump($a);Sample results :
// Merge $a $b $c
[
"a" => 30
"b" => [
"b.a" => 40
]
"c" => [
"c.a" => 50
"c.b" => 60
"c.c" => 100
]
]
// Merge $a $b
[
"a" => 20
"b" => 30
"c" => [
"c.a" => 40
"c.b" => 60
"c.c" => 100
]
]边栏推荐
- Build your own cloud game server. What if the cloud game server is attacked
- Junior network security engineers master these penetration methods and steadily improve their technology to become leaders!
- Question: can you get download the resources of Baidu online disk?
- What are the main functions of DNS? What are the benefits of IP address translation
- Benchmarking Shopify? Similarities and differences between "two giants" of Chinese e-commerce SAAS and Weimeng
- The difference between classless routing and classless routing
- Modify the original place where the method needs to be called and triggered
- Using robot framework to realize multi platform automated testing
- [untitled]
- What is a private VLAN? Eight part essay with pictures and texts.
猜你喜欢

BIM model example

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

How to fill in and register e-mail, and open mass mailing software for free

163 mailbox login portal display, enterprise mailbox computer version login portal

Introduction to development model + test model

Advanced BOM tool intelligent packaging function

Review of AI hotspots this week: the Gan compression method consumes less than 1/9 of the computing power, and the open source generator turns your photos into hand drawn photos

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations

application. Yaml configuring multiple running environments

Leetcode969: pancake sorting (medium, dynamic programming)
随机推荐
Tencent peace of mind ecological alliance was officially established as a linkage partner. Open technology helps "digital agriculture"
Frequent screen flashing after VNC login - abnormal system time
Select problem set 3
What is a region name? Can a territory name be used for trademark registration?
My eight year relationship with the message queue
How long can the trademark registration be completed? How to improve the speed of trademark registration?
Gin framework: adding tracing Middleware
How to use annotations to record operation logs gracefully
Use cloudflare to defend against DDoS for free
[new features] Tencent cloud lightweight ECS will soon support attaching data disks!!!
What are the main functions of DNS? What are the benefits of IP address translation
How to improve the success rate of trademark registration? How long does it take to register a trademark?
Cloud rendering: cloud exhibition hall of Tencent digital ecology Conference - open roaming mode on cloud
Leetcode969: pancake sorting (medium, dynamic programming)
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
SQL Server database recovery case analysis
What information should be provided for enterprise trademark registration? Is it difficult to register a trademark?
Cloudpods golang practice
[Tencent cloud double 12 audio and video communication special session] from 9 yuan for Q4 counter attack artifact, SMS and security (New) package!
How to build video websites? What are the types of video websites?