Home > Backend Development > PHP Tutorial > A case of using PHP custom function to implement array comparison function

A case of using PHP custom function to implement array comparison function

黄舟
Release: 2023-03-16 17:30:02
Original
1667 people have browsed it

This article mainly introduces the PHP custom function to implement the array comparison function, involving PHP's operation skills for array traversal, comparison, judgment and other related operations. Friends in need can refer to it

The examples of this article describe PHP Custom function implements array comparison function. Share it with everyone for your reference, the details are as follows:


<?php
 //数组使用标准比较运算符这样比较的
 function standard_array_compare($op1,$op2)
 {
 if(count($op1) < count($op2)) {
  return -1; //$op1 < $op2
 } else if(count($op1) > count($op1)) {
  return 1; //$op1 > op2
 }
 foreach ($op1 as $key => $val) {
  if(!array_key_exists($key,$op2)) {
  return null;
  } else if ($val < $op2[$key]) {
  return -1;
  } else if ($val > $op2[$key]) {
  return 1;
  }
 }
 return 0;
 }
 $arr1 = array(1,2,3,4,5);
 $arr2 = array(1,2,3,4,5);
 $arr3 = array(2,3,4,5,6);
 $arr4 = array(0,1,2,3,4);
 var_dump(standard_array_compare($arr1,$arr2));
 echo "<br/>";
 var_dump(standard_array_compare($arr1,$arr3));
 echo "<br/>";
 var_dump(standard_array_compare($arr1,$arr4));
?>
Copy after login

Running results:


int(0)
int(-1)
int(1)
Copy after login

The above is the detailed content of A case of using PHP custom function to implement array comparison function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template