亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

array_replace_recursive 覆蓋鍵而不是附加鍵

array_replace_recursive 覆蓋鍵而不是附加鍵

PHP
哈士奇WWW 2022-11-04 17:11:11
我試圖用另一個數組覆蓋或替換一個數組。為此,我嘗試了 array_replace_recursive 函數,但我得到了我似乎無法清除的不需要的行為。我有兩個數組:原始數組:[test] => Array             [me] => Array                     [0] => test                     [1] => me                     [2] => now             [me2] => Array                     [0] => test                     [1] => me                     [2] => now我要替換的數組[test] => Array             [me2] => Array                     [name] => firstname                     [last] => lastname使用 array_replace_recursive($first_array, $second_array) 后,我得到以下輸出: [test] => Array             [me] => Array                     [0] => test                     [1] => me                     [2] => now             [me2] => Array                     [0] => test <-- this needs to be overwritten not appended                     [1] => me <-- this needs to be overwritten not appended                     [2] => now <-- this needs to be overwritten not appended                     [name] => firstname                     [last] => lastname我真的需要覆蓋 ['test']['me2'] 值而不是附加。所以 0,1,2 鍵應該消失了。有人可以在這里指出我正確的方向嗎?
查看完整描述

2 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

您可能必須編寫自己的函數,以您認為合適的方式進行替換。我認為應該:

  • 接受兩個數組 ($a,$b) 作為參數

  • 如果 $a[KEY] 是一個數組且 $b[KEY] 未設置,則保留 $a[KEY]

  • 如果 $a[KEY] 是一個數組并且 $b[KEY] 是一個數組,調用這個方法 w/ ($a[KEY] & $b[KEY])

  • 如果 $a 的子節點不是數組且 $b 有子節點,則將 $a 替換為 $b

或者類似的東西......我很難概念化你到底需要什么,并為它編寫一個函數,我意識到有些極端情況可能會出現在更復雜的數組中。

所以我寫了這個函數和測試。我只使用您提供的示例數組對其進行了測試,但它提供了正確的輸出。如果您向數組添加另一層,或者其中有一個數組,其中有一些是數組的孩子和一些不是數組的孩子,這可能會出現問題。

<?php


function recurseReplace($a,$b){

    $ret = [];

    foreach ($a as $key=>$value){

        if (!isset($b[$key])&&is_array($value)){

            $ret[$key] = $value;

            continue;

        } 

        if (is_array($value)&&isset($b[$key])&&is_array($b[$key])){

            $ret[$key] = recurseReplace($value,$b[$key]);

            continue;

        }

    }

    if (count($ret)==0){

        foreach ($b as $key=>$value){

            $ret[$key] = $value;

        }

    }

    return $ret;

}


$a = [

    "test" => [

        "me"=>['test','me','now'],

        "me2"=>["test",'me','now']

        ]

    ];


$b = [

    "test" => [

        "me2"=>["name"=>'firstname',"last"=>"lastname"]

        ]

    ];


$desired = [

    "test" => [

        "me"=>['test','me','now'],

        "me2"=>["name"=>'firstname',"last"=>"lastname"]

        ]

    ];


$final = recurseReplace($a,$b);

echo "\n\n-----final output::---\n\n";


print_r($final);

echo "\n\n-----desired::---\n\n";


print_r($desired);


查看完整回答
反對 回復 2022-11-04
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

蘆葦,


謝謝你這對我有用..但我從你的代碼中得到啟發并開始改進它。對于其他需要這樣的人:


    function conf_get($paths, $array) {

        $paths = !is_array($paths) ? [] : $paths;

        foreach ($paths as $path)

            $array = $array[$path];

        return $array;

    }

    function conf_set($paths, $value, $array) {

        $array = !is_array($array) ? [] : $paths; // Initialize array if $array not set

        $result = &$array;

        foreach ($paths as $i => $path) {

            if ($i < count($paths) - 1) {

                if (!isset($result[$path]))

                    $result[$path] = [];

                $result = &$result[$path];

            } else

                $result[$path] = $value;

        }

        return $result;

    }


    $config = [];

    $config ['test']['me'] = ['test', 'me', 'now'];

    $config ['test']['me2'] = ['test', 'me', 'now'];


echo "\n INITIAL CONFIG"; 

print_r($config );

echo "\n GET PATH test"; 

print_r(conf_get('test', $config));

echo "\n GET PATH test,me1" ;

print_r(conf_get(['test', 'me2'], $config);

echo "\n REPLACE PATH test,me2 with new array" ;

print_r(conf_set(['test', 'me2'], ['name' => 'firstname', 'last' => 'lastname'], $config), "");

echo "\n ADD PATH test,me6 with new array";

print_r(conf_set(['test', 'me6'], ['name' => 'firstname', 'last' => 'lastname'], $config));

結果:



[INITIAL CONFIG]

    [test] => Array

            [me] => Array

                    [0] => test

                    [1] => me

                    [2] => now

            [me2] => Array

                    [0] => test

                    [1] => me

                    [2] => now


[GET PATH test]

    [test] => Array

            [me] => Array

                    [0] => test

                    [1] => me

                    [2] => now

            [me2] => Array

                    [0] => test

                    [1] => me

                    [2] => now


[GET PATH test,me1]

    [0] => test

    [1] => me

    [2] => now


[REPLACE PATH test,me2 with new array]

    [me2] => Array

            [name] => firstname

            [last] => lastname


[ADD PATH test,me6 with new array]

    [me6] => Array

            [name] => firstname

            [last] => lastname


查看完整回答
反對 回復 2022-11-04
  • 2 回答
  • 0 關注
  • 144 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號