Η συνάρτηση array_diff_assoc() του PHP

Παράδειγμα

Compare the keys and values of two arrays and return the difference set:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2 = array("a" => "red", "b" => "green", "c" => "blue");
$result=array_diff_assoc($a1,$a2);
print_r($result);
?>

Run Instances

Ορισμός και χρήση

Η συνάρτηση array_diff_assoc() χρησιμοποιείται για την σύγκριση των ονομάτων και των τιμών των κλειδιών δύο (ή περισσότερων) αριθμητικών διανυσμάτων και επιστρέφει την διαφορά.

This function compares the key names and key values of two (or more) arrays and returns a difference set array that includes all thearray1) but not in any other parameter array (array2 or array3 etc.) of the key names and key values.

Syntax

array_diff_assoc(array1,array2,array3...);
Parameters Description
array1 Required. The first array to compare with the other arrays.
array2 Required. The array to compare with the first array.
array3,... Optional. Other arrays to compare with the first array.

Technical Details

Return Value: Returns an array that contains all the array1 but not in any other parameter array (array2 or array3 etc.) of the key names and key values.
PHP Version: 4.3+

More Examples

Example 1

Compare the keys and values of two arrays and return the difference set:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff_assoc($a1,$a2);
print_r($result);
?>

Run Instances

Example 2

Compare the keys and values of three arrays and return the difference set:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","f"=>"green","g"=>"blue");
$a3=array("h"=>"red","b"=>"green","g"=>"blue");
$result=array_diff_assoc($a1,$a2,$a3);
print_r($result);
?>

Run Instances