1

i have 2 PHP files, config.php and confignw.php as follows,

config.php

$html = array(
    [update_item_in_store] => Array
        (
            [header] => default_header.php
            [body] => update_item_in_store.php
            [footer] => default_footer.php
        )

    [user_followed] => Array
        (
            [header] => default_header.php
            [body] => user_followed.php
            [footer] => default_footer.php
        )

    [updated_account_settings] => Array
        (
            [header] => default_header.php
            [body] => updated_account_settings.php
            [footer] => default_footer.php
        )
);

$cml = array
(
    "default_header",
    "default_body",
    "default_footer"
);

confignw.php

$html = array(
    [add_item_in_store] => Array
        (
            [header] => default_header.php
            [body] => add_item_in_store.php
            [footer] => default_footer.php
        )

    [user_followed] => Array
        (
            [header] => default_header.php
            [body] => user_followed_new.php
            [footer] => default_footer.php
        )
);

$cml = array
(
    "default_skeleton"
);

And both the files are included in a file called common.php,

And the result should come as merging of both as follows,

Expected Result:

$html = array(
    [update_item_in_store] => Array
        (
            [header] => default_header.php
            [body] => update_item_in_store.php
            [footer] => default_footer.php
        )

    [user_followed] => Array
        (
            [header] => default_header.php
            [body] => user_followed_new.php
            [footer] => default_footer.php
        )

    [updated_account_settings] => Array
        (
            [header] => default_header.php
            [body] => updated_account_settings.php
            [footer] => default_footer.php
        )
    [add_item_in_store] => Array
        (
            [header] => default_header.php
            [body] => add_item_in_store.php
            [footer] => default_footer.php
        )
);

$cml = array
(
    "default_header",
    "default_body",
    "default_footer",
            "default_skeleton"
);

Look at the value added in both arrays from the array in the file confignw.php and note that $html[user_followed][body] is changed. But what is happening is, only the second files' values are coming. So how to achieve this expected result? Any ideas or suggestion are welcome...

4

2 回答 2

3

PHP 不会神奇地合并数组。它遇到对同一个变量的两个赋值。在config.php您将一些数据分配给$html变量。包含文件时,会将数据分配给变量。然后,当confignw.php被包含时,PHP 为同一个$html变量分配另一个数据。没有合并,因为不应该有任何合并。

$a = array('a');
$a = array('b');
print_r($a); // prints array('b');

此代码演示了您在做什么。如果你想合并数组,你需要告诉 PHP。例如,confignw.php你可以写:

if (!isset($html)) {
    $html = array();
}
$html = array_merge($html, array(
    'add_item_in_store' => Array
        (
            'header' => 'default_header.php',
            'body' => 'add_item_in_store.php',
            'footer' => 'default_footer.php'
        ),

    'user_followed' => Array
        (
            'header' => 'default_header.php',
            'body' => 'user_followed_new.php',
            'footer' => 'default_footer.php'
        )
));

如果上面的代码没有做你想要的,请查看array_merge_recursive函数。

于 2013-03-11T08:21:07.987 回答
1

因为您包含具有相同名称变量的代码,所以我想后者将“覆盖”第一个变量。有点像把一张纸放在另一张纸上——你只会看到最上面的一张。

如果要合并这些数组,则需要将它们分配给不同名称的变量,然后按照 DCoder 的建议使用 array_merge_recursive。您还可以查看 array_replace_recursive 函数——它应该可以按您的意愿工作。检查这个

于 2013-03-11T08:22:09.643 回答