Obtaining steps: 1. Use the array_diff() function to compare two arrays, which will return a difference array containing different elements. The syntax is "array_diff(array 1, array 2)"; 2. Use count() The function counts the number of elements in the difference array, and the syntax is "count(difference array)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php Get two The number of different elements in the array
In PHP, you can use the array_diff() and count() functions to get the number of different elements in the two arrays.
Step 1: Use the array_diff() function to compare arrays and return the difference between the two arrays
The array_diff() function compares the key values of the array and returns a value containing Difference array of different elements
<?php header("Content-type:text/html;charset=utf-8"); $arr1=array(1,2,3,4,5,6); $arr2=array(1,2,4,6,8,9); var_dump($arr1); var_dump($arr2); echo "差集数组:"; $result=array_diff($arr1,$arr2); var_dump($result); ?>
Step 2: Use the count() function to count the number of elements in the difference array
## The #count() function can count the number of all elements in an array.<?php header("Content-type:text/html;charset=utf-8"); $arr1=array(1,2,3,4,5,6); $arr2=array(1,2,4,6,8,9); var_dump($arr1); var_dump($arr2); echo "差集数组:"; $result=array_diff($arr1,$arr2); var_dump($result); $len=count($result); echo "不同元素的个数为:".$len; ?>
Description:
array_diff() function is used to compare two (or more ) value of the array and returns the difference set. This function compares the values of two (or more) arrays (key=>value in value), and returns a difference array, which includes all the arrays being compared (array1 ), but not in any other parameter array (array2). Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get the number of different elements in two arrays in php. For more information, please follow other related articles on the PHP Chinese website!