1

次のように、config.phpとconfignw.phpの2つのPHPファイルがあります。

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"
);

そして、両方のファイルはcommon.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_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"
);

ファイル内の配列から両方の配列に追加された値をconfignw.php確認し、$ html[user_followed][body]が変更されていることに注意してください。しかし、何が起こっているのかというと、2番目のファイルの値だけが来ています。では、この期待される結果をどのように達成するのでしょうか?任意のアイデアや提案を歓迎します...

4

2 に答える 2

3

PHPは配列を魔法のようにマージしません。同じ変数への2つの割り当てが発生します。変数にいくつ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

同じ名前の変数を持つコードを含めているので、後者は最初の変数を「上書き」すると思います。紙を別の紙の上に置くのが好きです。上にあるのは1枚だけです。

これらの配列をマージする場合は、DCoderが提案したように、それらを異なる名前の変数に割り当ててから、array_merge_recursiveを使用する必要があります。array_replace_recursive関数をチェックアウトすることもできます-それはあなたが望むように動作するはずです。これを確認してください

于 2013-03-11T08:22:09.643 に答える