PHP - 比较两个多维数组 [英] PHP - Comparing two multidimensional arrays

查看:108
本文介绍了PHP - 比较两个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个阵列,在他们的数据,我需要比较两个并创建一个最终的阵列。这里是我的情况:

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
            )

    )

我需要这两个数组比较。如果有一个存在的顶级阵列和第二阵列(Github上克隆),那么我需要删除它不存在。如果有一个存在不顶数组中存在的底部阵列(旧),那么我需要添加它。我想我需要用新的数据的第三阵列,但我不知道如何组织这一点。

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.

此外,如果有在两个阵列的两个条目(MagentoPlayground)我需要的新的数组,以从底部阵列使用的数据。底部阵列将拥有最最新LAST_MODIFIED邮票目录大小。

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.

感谢您的帮助。

推荐答案

我比较使用你刚才提到的规则:

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


  • 存在于一个,但不是在B - >删除

  • 存在于B,而不是在A - >添加

...,并创建第三和最后一个数组。由于第一条规则,您可以通过排列B以及环作为比较,这将解决一个。

...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;
}

?>

单独从这两个规则看来,你可能也只是把你的第二个数组,因为如果在两个数组存在,它可以留下,如果没有B中你要删除它存在,但它不存在无论如何,如果它存在于B,而不是A你加它,但它已经存在...

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...

使用m_array_key_exists如上检查一个级别比像你曾表示阵列()是否存在数组键array_key_exists更深。如果您的规则并不像我想他们,就像你通过你的第二个数组要循环听起来对我来说,检查数组键,应用你的特殊规则和结果添加到第三个数组一样简单。

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.

这篇关于PHP - 比较两个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆