dqu92800
2012-02-02 17:35 阅读 89
已采纳

array_udiff_assoc和array_diff_uassoc之间的区别是什么?

what is the difference between array_udiff_assoc and array_diff_uassoc

for array_udiff_assoc i get this thing

<?php
 function myfunction($v1,$v2)
 {
 if ($v1===$v2)
   {
   return 0;
   }
 return 1;
 }
 $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
 $a2=array("a"=>"Cat","b"=>"Horse","c"=>"Dog");
 print_r(array_udiff_assoc($a1,$a2,"myfunction"));
 ?>

result

Array ( [b] Dog [c] => Horse )

also array_diff_uassoc

  <?php
     function myfunction($v1,$v2)
     {
     if ($v1===$v2)
       {
       return 0;
       }
     return 1;
     }
     $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
     $a2=array("a"=>"Cat","b"=>"Horse","c"=>"Dog");
     print_r(array_diff_uassoc($a1,$a2,"myfunction"));
     ?>

result is same as first one

Array ( [b] Dog [c] => Horse )

Is the have any difference, if have what is that. Php manual does not says that they are alias, as they used to say.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doulianqi3870 doulianqi3870 2012-02-02 17:43

    They both do the same, but udiff-assoc compares the DATA with the user supplied function, while diff-uassoc compares the INDEX with the user supplied function.

    As an answer to @lonsesomeday : as indicated by the 'u', diff_assoc will use internal functions for all comparisons, and udiff_uassoc uses provided callbacks for index and data comparison.

    http://www.php.net/manual/en/function.array-diff-uassoc.php

    http://www.php.net/manual/en/function.array-udiff-assoc.php

    点赞 2 评论 复制链接分享
  • douzhang2092 douzhang2092 2012-02-02 17:45

    array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
    array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function

    So, the function differ in the place where they use the callback function. udiff_assoc uses the callback to compare elements, diff_uassoc uses the callback when comparing the indices.

    点赞 评论 复制链接分享

相关推荐