Php judges that an array is a subset of another array.

Source: Internet
Author: User
Preface in the process of completing an algorithm today, there are several required modules in which we can determine whether the $ a array is a subset of the $ B array. maybe I have written many c statements recently, the for loop is implemented directly, but the code volume is large and not elegant enough! After brainstorming in the QQ group, I found many php scripts... "/> <scripttype =" text/javascript "src =" http: // www

Preface
During the process of completing an algorithm today, there are several required modules in which we can determine whether the $ a array is a subset of the $ B array. maybe I have written many c statements recently, the for loop is implemented directly, but the code volume is large and not elegant enough! I made a wide discussion in the QQ group and found that many system functions provided by php are available for calling. record them here.


Requirement
Determine whether the $ a array is a subset of the $ B array based on the minimum time complexity.
[Php]
// Quickly determine whether the $ a array is a subset of the $ B array
$ A = array (135,138 );
$ B = array (135,138,137 );

// Quickly determine whether the $ a array is a subset of the $ B array
$ A = array (135,138 );
$ B = array (135,138,137 );

Implementation method
The three methods are introduced here, with the same idea. The difference lies in the implementation code.


For loop traversal
[Php]
$ Flag = 1;
Foreach ($ a as $ va ){
If (in_array ($ va, $ B )){
Continue;
} Else {
$ Flag = 0;
Break;
}
}
 
If ($ flag ){
Echo "Yes ";
} Else {
Echo "No ";
}

$ Flag = 1;
Foreach ($ a as $ va ){
If (in_array ($ va, $ B )){
Continue;
} Else {
$ Flag = 0;
Break;
}
}

If ($ flag ){
Echo "Yes ";
} Else {
Echo "No ";
}


Use of array_diff

 

 


Code
[Php]
$ C = array_diff ($ a, $ B );
Print_r ($ c );
$ Flag = empty ($ c )? 1: 0;
 
If ($ flag ){
Echo "Yes ";
} Else {
Echo "No ";
}

$ C = array_diff ($ a, $ B );
Print_r ($ c );
$ Flag = empty ($ c )? 1: 0;

If ($ flag ){
Echo "Yes ";
} Else {
Echo "No ";
}


Use of array_intersect

 


Code
[Php]
If ($ a = array_intersect ($ a, $ B )){
$ Flag = 1;
} Else {
$ Flag = 0;
}
 
If ($ flag ){
Echo "Yes ";
} Else {
Echo "No ";
}

If ($ a = array_intersect ($ a, $ B )){
$ Flag = 1;
} Else {
$ Flag = 0;
}

If ($ flag ){
Echo "Yes ";
} Else {
Echo "No ";
}


Postscript
A good mentor can not only teach me how to learn, but also teach me how to do things with gratitude and responsibility.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.