Home > Backend Development > PHP Tutorial > Comparison of array_diff_assoc and array_diff function instances in PHP

Comparison of array_diff_assoc and array_diff function instances in PHP

巴扎黑
Release: 2023-03-07 21:00:02
Original
1963 people have browsed it

I mentioned array_diff before, this time it is its upgraded version, the PHP array difference comparison array_diff_assoc function is used to compare the differences between keys and values ​​in different arrays and output them according to the index array.

The usage syntax is the same as array_diff

$newarray = array_diff_assoc($array1,$array2....);

The same thing to note: array_diff_assoc cannot be compared recursively , can only be a one-dimensional array. If the array is nested within an array, the subsequent arrays will be compared according to "array", even if they are different arrays, they will be the same.

The difference is that this time there are more key values, that is, index values. If the indexes are different, they must be returned to the array.

Example:

1. Comparison with index

<?php
$array = array("key_name"=>"array","key_function"=>"array_diff_assoc");
$array1 = array("site"=>"forasp","name"=>"网站制作学习网","key_function"=>"array_diff_assoc");
$forasp = array_diff_assoc($array,$array1);
print_r($forasp);
?>
Copy after login

Result:

Array
(
[key_name] => array //也就是这个key-value 值没有在后面的数组中出现,是个不同值。
)
Copy after login

2. What if it is an array without index

<?php
$array = array("forasp","array","array_diff_assoc");
$array1 = array("forasp","网站制作学习网","array_diff_assoc");
$forasp = array_diff_assoc($array,$array1);
print_r($forasp);
?>
Copy after login

The result is the same as above, the difference is that this key becomes an index

Array
(
[1] => array //也就是这个key-value 值没有在后面的数组中出现,是个不同值。
)
Copy after login

That is to say, in an array without an index, the index id and value are compared.

array_diff_assoc has been introduced. It is an upgraded version of array_diff.

The above is the detailed content of Comparison of array_diff_assoc and array_diff function instances in PHP. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template