How to compare two PHP multidimensional arrays

藏色散人
Release: 2019-02-01 17:31:59
Original
14989 people have browsed it


# 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:

"; print_r($color);
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!