Home  >  Article  >  Daily Programming  >  How to compare two PHP multidimensional arrays

How to compare two PHP multidimensional arrays

藏色散人
藏色散人Original
2019-01-16 15:28:5015243browse


# Determine whether two multi-dimensional arrays are equal, or what elements are different. We can achieve comparison through array_udiff() function, create_function() method, strcmp () and other functions in PHP.

How to compare two PHP multidimensional arrays

Now we will introduce the method of comparing two two-dimensional arrays with specific code examples.

The code example is as follows:

<?php

//多维数组比较
$color1 = array(array(&#39;Red&#39;,80),array(&#39;Green&#39;,70),array(&#39;white&#39;,60));
$color2 = array(array(&#39;Green&#39;,70),array(&#39;Black&#39;,95));

$color = array_udiff($color1,$color2,create_function(
&#39;$a,$b&#39;,&#39;return strcmp(implode("",$a),implode("",$b));&#39;)
);

echo "<pre class="brush:php;toolbar:false">";

print_r($color);

In this code, we can get the different array elements of the $color1 array compared to the $color2 array.

The output comparison results are as follows:

How to compare two PHP multidimensional arrays

Function introduction:

1. array_udiff: Use the callback function to compare the data to calculate the difference set of the array

array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) : array

Use the callback function to compare the data and calculate the difference between the arrays. Unlike array_diff(), the former uses built-in functions for data comparison.

Parameter array1 represents the first array. array2 second array. value_compare_func callback comparison function.

When the first argument is less than, equal to, or greater than the second argument, the comparison function must return an integer that is less than, equal to, or greater than 0 accordingly.

callback (mixed $a, mixed $b): int

Return value, returns all values ​​in array1 that do not appear in other parameters.

2, create_function: Create an anonymous function

create_function ( string $args , string $code ): string

Create an anonymous function based on the passed parameters and assign it to Returns a unique name.

Normally these parameters will be passed as single quote delimited strings. The reason for using single quoted strings is to protect the variable name from being parsed, otherwise if you use double quotes, you would need to escape the variable name, such as \$avar.

args represents function parameters. code represents function code.

The return value is a unique function name returned in the form of a string or FALSE error.

3. strcmp: Binary safe string comparison

strcmp ( string $str1 , string $str2 ) : int

Note that this comparison is case-sensitive.

The parameter str1 represents the first string. str2 represents the second string.

The return value is if str1 is less than str2, return 0; if they are equal, return 0.

4. implode: Convert the value of a one-dimensional array into a string

This article is about the method of comparing two PHP multi-dimensional arrays, and it is also very useful. Simple and easy to understand, I hope it will be helpful to friends in need!


The above is the detailed content of How to compare two PHP multidimensional arrays. For more information, please follow other related articles on the PHP Chinese website!

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