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

Hello all, i have two arrays i.e

$ar1=array("Mobile","shop","software","hardware");

and

$arr2=arry("shop","Mobile","shop","software","shop")

i want to compare the elements of arr2 to arr1 i.e

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($val, $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

See how this goes for you, basically its just a recursive function that seeks out and increments a value based on its status in the array.

$array1 = array('Mobile', 'shop', 'software', 'hardware');
$array2 = array('shop', 'Mobile', 'shop', 'software', 'shop', array('shop', 'Mobile'));

function isInArray($array, $searchIn) {
    static $inc = array();
    
    foreach ($array as $v) {
        if (is_array($v) && sizeof($v) > 0) {
            isInArray($v, $searchIn);
        } else if (!is_array($v) && in_array($v, $searchIn)) {
            isset($inc[$v]) ? $inc[$v]++ : $inc[$v] = 1;
        }
    }
    
    return $inc;
}

$totals = isInArray($array2, $array1);
var_dump($totals);

Once the totals are complete for instance you can use…

$sql = "UPDATE `table_name` SET shop='" . $totals['shop'] . "'";


NOTE:
The above SQL query wouldn’t work is the array key shop doesn’t exist.

Thanks buddy, but look at my code if match is found first it insert data into mysql tables then increament the count for that specific item. So where should i first insert data in mysql table in your function.

Here is the reason why i said use the query after searching through the array, if you queried the database every time a result is found it would end up been redundant compared to using for instance a variable called $inc with the array key incremented.

i think you are not getting my point. look $arr1 is a predefined list of mysql tables and $arr2 is a json file got from API. First i loop through json array ($arr2) and match each element in arr1 if match found then i insert the match value name into my db table and also keep and increament this value for the update query. i just want to know how i can get a variable for each value in json so that i know how many times its match find.


<?php

$arr1 = array("Mobile","shop","software","hardware");
$arr2 = array("shop","Mobile","shop","software","shop");

// Get values from arr2 which exist in arr1
$overlap = array_intersect($arr2, $arr1);

// Count how many times each value exists
$counts  = array_count_values($overlap);

var_export($counts);

/*
$counts = array(
  'shop' => 3,
  'Mobile' => 1,
  'software' => 1,
);
*/

Then you just need to build your SQL query however you like. (:

References

i am not comparing both arrays. i just compare elements of $arr2 to $arr1 one by one and if match found insert into database and keep the variable for each element so that later i know how many times it matched.

My code gives you what you asked for (see the quoted part in my post). Can you explain more clearly what exactly you are trying to achieve?

Ok thats fine Salathe, now can i update the table dynamically i.e
for ‘shop’, ‘mobile’ and for ‘software’ like match field are automatically updated to database along with values.