Home  >  Article  >  Backend Development  >  PHP method to determine whether an array is a subset of another array

PHP method to determine whether an array is a subset of another array

WBOY
WBOYOriginal
2016-07-25 08:55:311622browse
  1. // Quickly determine whether $a array is a subset of $b array
  2. $a = array(135,138);
  3. $b = array(135,138,137);
Copy code

implementation method: Method 1, for loop traversal

  1. $flag = 1;
  2. foreach ($a as $va) {
  3. if (in_array($va, $b)) {
  4. continue;
  5. }else {
  6. $flag = 0;
  7. break;
  8. }
  9. }
  10. if ($flag) {
  11. echo "Yes";
  12. }else {
  13. echo "No";
  14. }
Copy code

Method 2, use of PHP method to determine whether an array is a subset of another array PHP method to determine whether an array is a subset of another array

Code:

  1. $c = PHP method to determine whether an array is a subset of another array($a, $b);
  2. print_r($c);
  3. $flag = empty($c)?1 : 0;
  4. if ($flag ) {
  5. echo "Yes";
  6. }else {
  7. echo "No";
  8. }
Copy code

Method 3, use of PHP method to determine whether an array is a subset of another array PHP method to determine whether an array is a subset of another array

Example:

  1. if ($a == PHP method to determine whether an array is a subset of another array($a, $b)) {
  2. $flag = 1;
  3. }else {
  4. $flag = 0;
  5. }
  6. if ($ flag) {
  7. echo "Yes";
  8. }else {
  9. echo "No";
  10. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn