Example of a PHP custom function to implement an array comparison function

Source: Internet
Author: User
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)

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.