Comparing the inside of a multidimensional array with a simple array

I am trying to compare the 'criteria' array inside this multidimensional array

   Array
(
    [8399] => Array
        (
            [qty] => 1
            [criteria] => Array
                (
                    [Marime] => 2
                    [Suport] => 6
                    [Pret] => 3.80
                )

        )

)

With this array:

Array
(
    [Marime] => 1
    [Suport] => 7
    [Pret] => 6.20
)

All this because I want to be able to do something like

if($array1 !== $array2){}

Any help would be appreciated , thank you!

4 answers

  • answered 2017-08-12 09:48 Shawinder Jit Singh

    $result = array_diff($array1[8399]['criteria'],$array2);

    print_R($result);

    try this code it will display the diffrence in both array values

  • answered 2017-08-12 09:48 Gyandeep Sharma

    Here is your solution...

    $m_array = array(
            8399 => array(
                'qty' => 1,
                'criteria' => array(
                    'Marime' => 2,
                    'Suport' => 6,
                    'Pret' => 3.80
                    )
                )
            );
    $array = array('Marime' => 1,'Suport' => 7, 'Pret' => 6.20);
    
    foreach($m_array as $row){
        if(($row['criteria']['Marime']==$array['Marime'])&&($row['criteria']['Suport']==$array['Suport'])&&($row['criteria']['Pret']==$array['Pret'])){
            //type your code here....
        }
    }
    echo '<pre>';print_r($m_array);
    

  • answered 2017-08-12 09:48 Kamran Jabbar

    Use this code below to compare the arrays.

    if(($aray1[8399]['criteria']['Marime']==$aray2['Marime'])&&($aray1[8399]['criteria']['Suport']==$aray2['Suport'])&&($aray1[8399]['criteria']['Pret']==$aray2['Pret'])){
    
        echo 'Your output'; 
    }
    

    Click here for demo

  • answered 2017-08-12 09:48 JBan321

    I used someone's answer that got deleted:

    if($products[$product->getId()]['criteria'] == $criteria)){
    
    }
    

    Thank you all!