This article mainly introduces how to merge two ordered arrays in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The case is as follows:
$arr1 = array(1,2,3,4,5,6,7,8); $arr2 = array(3,4,5,7,9,10); //方法1 function mergeOrderly1($arr1,$arr2){ $i=0;$j=0; $int = array(); while($i<count($arr1) && $j<count($arr2)){ $int[] = $arr1[$i]<$arr2[$j]?$arr1[$i++]:$arr2[$j++]; } while($i<count($arr1)){ $int[] = $arr1[$i++]; } while($j<count($arr2)){ $int[] = $arr2[$j++]; } //$int = array_merge($int,array_slice($arr1,$i)); //$int = array_merge($int,array_slice($arr2,$j)); return $int; } //方法2 function mergeOrderly2($arr1,$arr2){ $arr = array();//定义最终数组容器 $arr2Num = count($arr2);//统计数组长度 $arr1Num = count($arr1); $i1 = 0;//数组1 的循环标记 $i2 = 0;//数组2 的循环标记 while($i1 < $arr1Num || $i2 < $arr2Num){//是否还需要合并 if($i1 < $arr1Num && $i2 < $arr2Num){//当两个数组都没有到达末尾,情况一 if($arr1[$i1] > $arr2[$i2]){//需要比较数组1和数组2,小的放入目标数组,并且标记+1 $arr[] = $arr2[$i2]; $i2++; }else{ $arr[] = $arr1[$i1]; $i1++; } }elseif($i1 < $arr1Num && $i2 >= $arr2Num){//数组2 已经到达末尾,而数组1还为到达,情况二 $arr[] = $arr1[$i1];//直接把数组1的内容插入到目标数组中 $i1++; }elseif($i2 < $arr2Num && $i1 >= $arr1Num){//数组1已经到达末尾,而数组2还未到达,情况三 $arr[] = $arr2[$i2];//直接把数组2的内容插入到目标数组中 $i2++; } } return $arr; } print_r(mergeOrderly1($arr1,$arr2)); print_r(mergeOrderly2($arr1,$arr2));
Running results:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 3 [4] => 4 [5] => 4 [6] => 5 [7] => 5 [8] => 6 [9] => 7 [10] => 7 [11] => 8 [12] => 9 [13] => 10 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 3 [4] => 4 [5] => 4 [6] => 5 [7] => 5 [8] => 6 [9] => 7 [10] => 7 [11] => 8 [12] => 9 [13] => 10 )
Careful readers should have discovered that the merged arrays are not duplicated. The following introduces the method of deduplication after merging arrays:
1. Deduplication of single arrays
array_unique($arrTest)
2. Deduplication of multiple arrays
array_keys(array_flip($arr1)+array_flip($arr2))
Test example:
$arr1 = array(1,2,3,4,5); $arr2 = array(1,2,3,6,7); $arr3 = array('0'=>1,'1'=>2,'2'=>3,'3'=>4,'4'=>5); $arr4 = array('0'=>1,'1'=>2,'2'=>3,'3'=>6,'4'=>7); $arr5 = array('0'=>1,'a'=>2,'b'=>3,'c'=>4,'4'=>5); $arr6 = array('0'=>1,'a'=>2,'c'=>3,'d'=>6,'4'=>7); var_dump(array_merge($arr1, $arr2)); var_dump($arr1+$arr2); var_dump(array_keys(array_flip($arr1)+array_flip($arr2))); echo '<br>'; var_dump(array_merge($arr3, $arr4)); var_dump($arr3+$arr4); var_dump(array_keys(array_flip($arr3)+array_flip($arr4))); echo '<br>'; var_dump(array_merge($arr5, $arr6)); var_dump($arr5+$arr6);
Test results:
array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(1) [6]=> int(2) [7]=> int(3) [8]=> int(6) [9]=> int(7) } array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } array(7) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) } array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(1) [6]=> int(2) [7]=> int(3) [8]=> int(6) [9]=> int(7) } array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } array(7) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) } array(8) { [0]=> int(1) ["a"]=> int(2) ["b"]=> int(3) ["c"]=> int(3) [1]=> int(5) [2]=> int(1) ["d"]=> int(6) [3]=> int(7) } array(6) { [0]=> int(1) ["a"]=> int(2) ["b"]=> int(3) ["c"]=> int(4) [4]=> int(5) ["d"]=> int(6) }
Related recommendations:
Analysis of the method of merging two ordered arrays in PHP
Java has Analysis of ordinal array data structure and binary search algorithm
Introduction to the method of finding the minimum absolute value in an ordered array using PHP
The above is the detailed content of PHP merge two ordered array cases. For more information, please follow other related articles on the PHP Chinese website!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
