我试着写一个方法来递归填充来自2个多维数组的任何缺失键和空值,我一直在墙上撞了几天。
基本上,我有2个数组,一个是带有所有预期键的“主”多维数组,另一个是我想填写缺失值的数组(如果它们不存在)。
例如:
主阵列:
Array
(
[title] =>
[blog_content] =>
[blog_files] => Array
(
[0] => Array
(
[file] => Array
(
[file_id] =>
[site_id] =>
[entry_id] =>
[field_id] =>
[channel_id] =>
[member_id] =>
[member_info] => Array
(
[0] => Array
(
[username] =>
[member_id] =>
[screen_name] =>
[email] =>
[join_date] =>
[last_visit] =>
[group_id] =>
[in_authorlist] =>
)
)
[link_field_id] =>
[filename] =>
[extension] =>
[mime] =>
[upload_service] =>
[title] =>
[url_title] =>
[filesize] =>
[md5] =>
[file_primary] =>
[file_order] =>
[date] =>
[downloads] =>
)
)
)
Modded Array:
Array
(
[title] => Title goes here
[status] => open
[entry_date] => 1321038883
[entry_id] => 79
[blog_content] =>
[blog_files] =>
[test_file] =>
)
我要做的是获取主数组并将任何缺少的键和多维结构添加到modded数组中,使其如下所示:
Array
(
[title] => Title goes here
[status] => open
[entry_date] => 1321038883
[entry_id] => 79
[blog_content] => Here's the content
[blog_files] => Array
(
[0] => Array
(
[file] => Array
(
[file_id] =>
[site_id] =>
[entry_id] =>
[field_id] =>
[channel_id] =>
[member_id] =>
[member_info] => Array
(
[0] => Array
(
[username] =>
[member_id] =>
[screen_name] =>
[email] =>
[join_date] =>
[last_visit] =>
[group_id] =>
[in_authorlist] =>
)
)
[link_field_id] =>
[filename] =>
[extension] =>
[mime] =>
[upload_service] =>
[title] =>
[url_title] =>
[filesize] =>
[md5] =>
[file_primary] =>
[file_order] =>
[date] =>
[downloads] =>
)
)
)
我已经尝试了一些讨厌的头部爆炸递归(我的头部现在受伤),内置了像array_merge这样的函数(对于数字键不起作用),并且没有任何东西可以使事情正常工作。
有一点需要注意的是,有时修改后的数组有时会具有主数组的结构,或者结构的一部分,具有值,因此将主数据附加到modded将覆盖任何现有值。
任何帮助/建议都将不胜感激。
答案 0 :(得分:1)
这看起来非常适合经常被遗忘的array union operator
$newArray = $moddedArray + $masterArray;
我不知道你想用多维$moddedArray
做什么,因为你的例子并不清楚。