This article mainly introduces the PHP custom function to implement the array comparison function, involving PHP for array traversal, comparison, judgment and other related operations skills, the need for friends can refer to the following
This paper describes the function of PHP custom function to implement array comparison. Share to everyone for your reference, as follows:
<?php//arrays Use the standard comparison operator to compare function Standard_array_compare ($op 1, $op 2) {if (count ($op 1) < count ($op 2)) { return -1; $op 1 < $op 2} else if (count ($op 1) > Count ($op 1)) { return 1;//$OP 1 > OP2} foreach ($op 1 as $key + = $va L) { if (!array_key_exists ($key, $op 2)) { return null; } else if ($val < $op 2[$key]) { return-1; } else if ($val > $op 2[$key]) { return 1;}} return 0;} $arr 1 = array (1,2,3,4,5); $arr 2 = array (1,2,3,4,5); $arr 3 = array (2,3,4,5,6); $arr 4 = array (0,1,2,3,4); Var_dump (Standard_array_compare ($arr 1, $arr 2)); echo "<br/>"; Var_dump (Standard_array_compare ($arr 1, $arr 3)); echo "<br/>"; Var_dump (Standard_array_compare ($arr 1, $arr 4));? >
Operation Result:
int (0) int ( -1) int (1)