0

几天来,我一直在用头撞墙,试图编写一种方法来递归地从 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] => 
                        )

                )

        )

修改后的数组:

Array
(
    [title] => Title goes here
    [status] => open
    [entry_date] => 1321038883
    [entry_id] => 79
    [blog_content] => 
    [blog_files] => 
    [test_file] => 

)

我要做的是获取主数组并将任何丢失的键和多维结构添加到修改后的数组中,如下所示:

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 之类的函数(不适用于数字键),但没有什么能让事情正常进行。

但是要记住的一件事是,有时修改后的数组有时会具有主数组的结构或结构的一部分,其值因此将主数组附加到修改后的数组将覆盖任何现有值。

任何帮助/建议将不胜感激。

4

1 回答 1

1

这看起来非常适合经常被遗忘的数组联合运算符

$newArray = $moddedArray + $masterArray;

我不知道你想用 multi-dimensional $moddedArrays 做什么,因为你的例子并不清楚。

于 2012-06-01T18:22:02.253 回答