简体   繁体   中英

PHP - Comparing two multidimensional arrays

I have two arrays with data in them and I need to compare the two and create one final array.. here is my situation:

// grab a list of the folders
$folders = glob("../*",GLOB_ONLYDIR);

// create empty array's which will contain our data
$projects_data = array();
$folders_array = array();

// list the contents of the config file
$data = json_decode(file_get_contents('.my-config'), true);

// loop through our data file
foreach($data['web_app']['projects'] as $project) :
  // update our projects data array
  $projects_data[] = $project;
endforeach;

// loop through each folder on our localhost
foreach($folders as $folder) :
  // update our folders array
  $folders_array[] = array(
    'folder' => basename($folder),
    'last_modified' => filemtime($folder),
    'dir_size' => dirsize($folder)
  );
endforeach;

so I have two arrays.. like so:

    $projects_data array
    Array
    (
        [0] => Array
            (
                [folder] => GitHub Clones
                [last_modified] => 1379974689
                [dir_size] => 6148
            )

        [1] => Array
            (
                [folder] => MagentoPlayground
                [last_modified] => 1380336582
                [dir_size] => 82340978
            )

        [2] => Array
            (
                [folder] => Projects
                [last_modified] => 1380581312
                [dir_size] => 5954
            )
    )

    $folders_array array
    Array 
    (
        [0] => Array
            (
                [folder] => MagentoPlayground
                [last_modified] => 1380336582
                [dir_size] => 82340978
            )

        [1] => Array
            (
                [folder] => Projects
                [last_modified] => 1380581312
                [dir_size] => 5933
            )

        [2] => Array
            (
                [folder] => old
                [last_modified] => 1371064970
                [dir_size] => 63385844
            )

    )

I need to compare these two arrays.. If there is one that exists in the top array and does not exist in the second array (Github Clones) then I need to remove it. If there is one that exist in the bottom array that does not exist in the top array (old) then I need to add it. I guess I will need a third array with the new data but I'm not sure how to structure this.

Also, if there are two entries in both arrays (MagentoPlayground) I need the new array to use the data from the bottom array. The bottom array will have the most up to date last_modified stamp and directory size.

Thanks for any help.

I'd compare using the rules you've just mentioned:

  • Exists in A but not in B -> remove
  • Exists in B but not in A -> add

...and create a third and final array. Due to the first rule, you may as well loop through array B as comparison which will solve that one.

<?php

// multidimensional array key search (one deep)
function m_array_key_exists($key, $array) {
    foreach($array as $subkey => $subvalue) {
        if($subkey === $key) 
            return true;
        if(is_array($subvalue)){
            if(array_key_exists($key, subvalue))
                return true;
        }
    }
    return false;
}

?>

Seems from those two rules alone that you may as well just take your second array, because if it exists in both arrays it can stay, if it doesn't exist in B you are going to remove it, but it's not there anyway, and if it exists in B but not A you add it, but it's already there...

Use m_array_key_exists as above to check one level deeper than array_key_exists() whether an array key exists in arrays like you've shown. If your rules aren't as simple as I've thought they are, it sounds to me like you want to loop through your second array, check for array keys, apply your special rules and add the result to the third array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM