当前位置:网站首页>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
]
]边栏推荐
- Xmlmap port practice - X12 to CSV
- The new purchased machines with large customized images are slow to enter the system
- How to fill in and register e-mail, and open mass mailing software for free
- How to use the cloud game server is the cloud server stable
- Talk about 15 tips of SQL optimization
- What is the cloud desktop server configuration? What are the application scenarios of cloud desktop?
- What is raid? 2000 words can explain RAID 0, 1, 5 and 10 thoroughly, and collect!
- How to calculate the trademark registration fee? How much does it cost to register a trademark?
- Mysql Find_ IN_ Set function
- Modify the original place where the method needs to be called and triggered
猜你喜欢

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

Leetcode969: pancake sorting (medium, dynamic programming)

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

Introduction to development model + test model

application. Yaml configuring multiple running environments

BIM model example

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

Advanced BOM tool intelligent packaging function

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

layer 3 switch
随机推荐
The whole procurement process of the procurement mall website in the transportation industry is electronic to reduce the procurement cost
WordPress site quickly integrates Tencent's digital identity management and control platform CIAM to realize login authentication without development
How to recover the garbled words in the software?
Tencent cloud double 11 Live Room activity rules
Embedded hardware development tutorial -- Xilinx vivado HLS case (3)
Cloud rendering: cloud exhibition hall of Tencent digital ecology Conference - open roaming mode on cloud
Hungry? Remote dual live database practice
Tencent peace of mind ecological alliance was officially established as a linkage partner. Open technology helps "digital agriculture"
Cloudpods golang practice
[tcapulusdb knowledge base] manually view the online operation of tcapulusdb
Shopify has quietly taken the second place in e-commerce in North America. Is independent station the "magic weapon" to win?
IP address and subnet partition are very important. This article is very fragrant!
Tencent cloud won the first place in the cloud natural language understanding classification task
Build your own cloud game server. What if the cloud game server is attacked
NFT metauniverse and the relationship between Games Golden Finance
How to confirm whether IPv6 is enabled for a website
What is a region name? Can a territory name be used for trademark registration?
The core battlefield of China US AI arms race: trillion level pre training model
How to build a video website? How much does it cost to build a video website?
Initial experience of creating partitioned tables under MySQL cases tdsql for MySQL (I)