Συνάρτηση array_diff_ukey() του PHP
Παράδειγμα
Σύγκριση των κλειδιών δύο πινάκων (χρησιμοποιώντας χρησιμοποιούμενη συνάρτηση χρήστη για τη σύγκριση των κλειδιών) και επιστροφή διαφοράς:
<?php function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"blue","b"=>"black","e"=>"blue"); $result=array_diff_ukey($a1,$a2,"myfunction"); print_r($result); ?>
Ορισμός και χρήση
Η συνάρτηση array_diff_ukey() χρησιμοποιείται για τη σύγκριση των κλειδιών δύο (ή περισσότερων) πινάκων και επιστρέφει διαφορά.
Σημείωση:Η συνάρτηση αυτή χρησιμοποιεί χρησιμοποιούμενη συνάρτηση χρήστη για τη σύγκριση των κλειδιών!
Η συνάρτηση αυτή συγκρίνει δύο (ή περισσότερους) πίνακες κλειδιών και επιστρέφει έναν πίνακα διαφοράς που περιλαμβάνει όλους τους πίνακες που συγκρίνονται (array1) but not in any other parameter array (array2 or array3 etc.) key names.
Γλώσσα
array_diff_ukey(array1,array2,array3...,myfunction);
Παράμετροι | Περιγραφή |
---|---|
array1 | Απαιτείται. Ο πρώτος πίνακας που συγκρίνονται με άλλους πίνακες. |
array2 | Απαιτείται. Ο πίνακας που συγκρίνονται με τον πρώτο πίνακα. |
array3,... | Οπτιμαλιστικό. Άλλες πίνακες που συγκρίνονται με τον πρώτο πίνακα. |
myfunction | Απαιτείται. Ο χαρακτηρισμός της αλφαβητικής αλυσίδας που ορίζει τη συγκριτική συνάρτηση που μπορεί να καλείται. Αν ο πρώτος παράγοντας είναι μικρότερος, ίσος ή μεγαλύτερος από τον δεύτερο παράγοντα, η συγκριτική συνάρτηση πρέπει να επιστρέφει ακέραιο αριθμό μικρότερο, ίσο ή μεγαλύτερο από το 0. |
Description
array_diff_ukey() returns an array that includes all the key names that are present in array1 but not present in any other parameter array. Note that the association relationship is preserved. Unlike array_diff(), the comparison is based on key names rather than values.
This comparison is done through a callback function provided by the user. An integer less than, equal to, or greater than zero must be returned respectively when the first parameter is considered to be less than, equal to, or greater than the second parameter.
Technical Details
Return Value: | Returns the difference set array, which includes all the key names that are present inarray1) but not in any other parameter array (array2 or array3 etc.) key names. |
PHP Version: | 5.1+ |
More Examples
Example 1
Compare the key names of three arrays (using a user-defined function to compare key names) and return the difference set:
<?php function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"black","b"=>"yellow","d"=>"brown"); $a3=array("e"=>"purple","f"=>"white","a"=>"gold"); $result=array_diff_ukey($a1,$a2,$a3,"myfunction"); print_r($result); ?>