php compare two associative arrays

前端 未结 1 342
失恋的感觉
失恋的感觉 2020-12-16 00:05

i have these two associative arrays

// the needle array

$a = array(
\"who\" => \"you\", 
\"what\" => \"thing\", 
\"where\" => \         


        
相关标签:
1条回答
  • 2020-12-16 00:47

    you can look into the php's array_diff_assoc() function or the array_intersect() function.

    EDIT

    Here's a sample on counting the matched values:

    <?php
      $a = array(
        "who" => "you", 
        "what" => "thing", 
        "where" => "place",
        "when" => "hour"
      );
      // the haystack array
      $b = array(
        "when" => "time", 
        "where" => "place", 
        "who" => "you",
        "what" => "thing"
      );
      $c = count(array_intersect($a, $b));
      echo $c;
    ?>
    

    CODEPAD link.

    0 讨论(0)
提交回复
热议问题