how to compare two arrays and find the count of match elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semanticnotion
    New Member
    • Sep 2010
    • 66

    #1

    how to compare two arrays and find the count of match elements

    Hello all, i have two arrays i.e
    Code:
    $ar1=array("Mobile","shop","software","hardware");
    and
    Code:
    $arr2=arry("shop","Mobile","shop","software","shop")
    i want to compare the elements of arr2 to arr1 i.e
    Code:
    foreach($arr2 as $val)
        { 
            if(in_array($val, $arr1))
            {    
                //mycode to insert data into mysql table
    
                variable++;  // here there should be a variable that must be increamented when ever match found in $arr2 so that it can be saved into another table.
            }
             $query="update table set shop='$variable',Mobile='$variable'.......";
         }
    my problem is when ever a match is found in if(in_array($va l, $arr1) my code will be executed. here i want a variable or an array to count how many times that matched item/element is found i.e for shop=3 and for mobile and software it should be 1. Later in my update query i want to set the fields with this count
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    Try array_count_val ues()
    see: http://php.net/manual/en/function.ar...unt-values.php

    Comment

    Working...