Compare the keys and values of two arrays (using two user-defined functions for comparison) and return the matches:


SUBMITTED BY: henry1874w

DATE: June 26, 2017, 4:54 p.m.

FORMAT: Text only

SIZE: 412 Bytes

HITS: 260

  1. <?php
  2. function myfunction_key($a,$b)
  3. {
  4. if ($a===$b)
  5. {
  6. return 0;
  7. }
  8. return ($a>$b)?1:-1;
  9. }
  10. function myfunction_value($a,$b)
  11. {
  12. if ($a===$b)
  13. {
  14. return 0;
  15. }
  16. return ($a>$b)?1:-1;
  17. }
  18. $a1=array("a"=>"red","b"=>"green","c"=>"blue");
  19. $a2=array("a"=>"red","b"=>"green","c"=>"green");
  20. $result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value");
  21. print_r($result);
  22. ?>

comments powered by Disqus