1

In php, i am new in php anybody help me for this? i have two arrays , in Array2 i have two records, i want to check Data of Array2 whether in Array1 or not , how can i check the data of Array2 in Array1 its available or not !

Thanks in advance

Array1

    [items] => Array
    (
        [0] => Array
            (
                [abc] => z1
                [xyz] => cool
                [val] => 2.32
                [color] => D
             )
         [1] // i have 5o records in array1
     );

Array 2

    [items] => SearchArray
    (
        [0] => Array
            (
                [abc] => z1
                [xyz] => cool
                [val] => 2.32
                [color] => D
             )
         [1] // i have 2 records
     );
4
  • 1
    read about the function in_array() php.net/manual/en/function.in-array.php Commented Jan 3, 2015 at 3:12
  • @chepe263 thanks you for your replay i don't want this way,i want to do Search Data of SearchArray['items'] from array1['items'] how can i do this ? Commented Jan 3, 2015 at 3:15
  • you could loop from one of the arrays, check if abc from array1 is in array2. If true, check the values. Commented Jan 3, 2015 at 3:16
  • @chepe263 yes i tried using loop but i don't get the results of this Commented Jan 3, 2015 at 3:21

2 Answers 2

2

Please try this code - I hope it helpes some way :

$matches = array();

for($i2 = 0; $i2 < count($Array2); $i2++)
{
    for($i1 = 0; $i1 < count($Array1); $i1++)
    {
        $bMatch = TRUE;
        foreach($Array1[$i1] as $key => $val)
        {
            if($Array2[$i2][$key] !== $val)
            {
                $bMatch = FALSE;
                break;
            }
        }
        if($bMatch)
        {
            $matches[] = array($i2, $i1);
        }
    }
}

It iterates through both arrays, comparing elements (which in fact are sub arrays) in such way that they are equal only if all elements of sub array from $Array2 are equal to all elements of sub array from $Array1. If the match is found, the pair ($i2, $i1) is added to the $matches array, so in the end, basing on your example, you would have something like:

$matches => array  (
               [0] => array (0, 0)
               ...
            )

I hope the assumption made is the proper one.

Sign up to request clarification or add additional context in comments.

2 Comments

if($Array2[$key] !== $val){$bMatch = FALSE; break;} i getting error in this line in which i don't get key from array2
I;m really glad could help
0

Can use array_search method. For more information, check out the manual reference: http://php.net/manual/en/function.array-search.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.