我有兩個陣列,在他們的數據,我需要比較兩個並創建一個最終的陣列..是我的情況:PHP - 在這裏比較兩個多維數組
// 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;
所以我有兩個數組..像這樣:
$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的克隆),然後我需要刪除它。如果底層數組中存在一個頂層數組中不存在的數據(舊數據),那麼我需要添加它。我想我需要第三個數組與新的數據,但我不知道如何構造這個。另外,如果兩個數組(MagentoPlayground)中有兩個條目,我需要新的數組使用底部數組中的數據。底部數組將具有最新的last_modified標記和目錄大小。
感謝您的任何幫助。
你應該讀這個和其他親屬函數http://php.net/manual/en/function.array-diff.php –