LinuxVirgin
LinuxVirgin

Reputation: 35

matching 2 multidimensional arrays comparison

I have 2 arrays, dataArray() has been generated from a simple HTML DOM parser. If I print_r the $dataArray I get the following output

Array ( 
[0] => Array ( ) 
[1] => Array ( 
    [0] => 'value1a'
    [1] => 80
    [2] => '2016-06-14 16:40:51'
    [3] => ) 
[2] => Array ( 
    [0] => 'value1b'
    [1] => 80
    [2] => '2016-06-14 16:30:17' 
    [3] => ) 
[3] => Array ( 
    [0] => 'value1c'
    [1] => 80
    [2] => '2016-06-14 16:40:51'
    [3] => )
    )

and searchArray() which is generated dynamically and if I print_r the searchArray() I get the following result:

Array (
[0] => Array ( 
    [0] => 'value1c'
    [1] => 80
    [2] => '2016-06-14 16:40:51'
    [3] => ) 
)

I'm trying to match searchArray() to dataArray() and give a Boolean result of true if (all 3 values) searchArray() is matched to (all 3 values) dataArray()

So in this case it would be a true statement.

Upvotes: 1

Views: 39

Answers (2)

xconspirisist
xconspirisist

Reputation: 1461

You can use the php function array_search (http://php.net/array_search) for this, because in testing search/key equality, PHP will check the entire array's contents to check that they are equal. Here is my test case;

<?php                                                                              

$dataArray = array(                                                                
    array(),                                                                       
    array(                                                                         
        'value1a',                                                                 
        80,                                                                        
        '2016-06-14 16:40:51',                                                     
        null                                                                       
    ),                                                                             
    array(                                                                         
        'value1b',                                                                 
        80,                                                                        
        '2016-06-14 16:30:17',                                                     
        null                                                                       
    ),                                                                             
    array(                                                                         
        'value1c',                                                                 
        80,                                                                        
        '2016-06-14 16:40:51',                                                     
        null                                                                       
    )                                                                              
);                                                                                 

$searchArray = array(                                                              
    'value1c',                                                                     
    80,                                                                            
    '2016-06-14 16:40:51',                                                         
    null                                                                           
);                                                                                 

var_dump(array_search($searchArray, $dataArray)); // int(3) (the found key)        

$searchArray[0] = 'some other value';                                              

var_dump(array_search($searchArray, $dataArray)); // false                         

?>   

Upvotes: 1

Jignesh Patel
Jignesh Patel

Reputation: 1022

You need to write custom function to match array recursovely.

you can check solution mentioned in below post which will help you a lot.

https://www.daniweb.com/programming/web-development/threads/168654/compare-multi-dimensional-arrays-and-return-they-keys-who-s-values-are-different

Upvotes: 1

Related Questions