第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

根據(jù)一個數(shù)組中的值替換另一個數(shù)組中的值

根據(jù)一個數(shù)組中的值替換另一個數(shù)組中的值

PHP
哈士奇WWW 2023-07-15 16:49:54
我有以下數(shù)組$priceYear = Array (        [AZ] => Array              (                [1] => 2020                [2] => 2020              )        [BY] => Array              (                [0] => 2020                [1] => 2020              )        [CX] => Array              (                [1] => 2020               [2] => 2020                [3] => 2020              )        [DW] => Array              (                [106] => 2019                [107] => 2019                [108] => 2019             )      ) 還有另一個數(shù)組$array = Array (        [0] => Array              (                [YEAR] => 2018                [VALUE_AMDON] => 55              )        [1] => Array              (                [YEAR] => 2019               [VALUE_AMDON] => 57             )        [2] => Array              (                [YEAR] => 2020                [VALUE_AMDON] => 59             )       ) 如果 $priceYear == YEAR 中的值我想替換為 VALUE_AMDON所以輸出應該是這樣的$priceYear = Array (                     [AZ] => Array                           (                             [1] => 59                             [2] => 59                          )                     [BY] => Array                           (                             [0] => 59                            [1] => 59                           )                     [CX] => Array                           (                             [1] => 59                            [2] => 59                             [3] => 59                           )                     [DW] => Array                           (                             [106] => 57                            [107] => 57                             [108] => 57                          )             ) 但不幸的是它不起作用,它返回初始數(shù)組如果有人可以幫助我,看起來這個錯誤非常愚蠢,但我沒有看到它:c
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

您還可以使用數(shù)組函數(shù)(例如 array_column 和 array_search 檢查如下)來完成此操作:


$array_year = array_column($array, "YEAR");

foreach($priceYear as $key => $val){

    foreach($val as $innerkey => $innerval){

        // Below If year exist in $array_year function return key of $array_year else return false

        $isExistKey = array_search($innerval, $array_year);

        if($isExistKey !== FALSE){

            $priceYear[ $key ][ $innerkey ] = $array[ $isExistKey ]["VALUE_AMDON"];

        }

    }

}

echo "<pre>";

print_r($priceYear);

在此處檢查輸出:https://paiza.io/projects/gUNihGow6-CWO0eqWpEtxg ?language=php


查看完整回答
反對 回復 2023-07-15
?
楊__羊羊

TA貢獻1943條經(jīng)驗 獲得超7個贊

由于數(shù)組的鍵之間沒有直接鏈接,因此最簡單的方法是使用嵌套循環(huán)。


您只需要循環(huán)遍歷$priceYear數(shù)組并檢查每個“年份”值和YEAR中的值$array。如果它們匹配,您可以僅替換$priceYear數(shù)組中的該值。


下面的代碼帶有注釋,可以告訴您每一行的作用:


// 1. loop through the nested arrays in $priceYear

foreach ($priceYear as $key => $price_years_array) {

    foreach ($price_years_array as $index => $price_years_value) {


        // 2. Create a variable to store the new value for the year in this $priceYear array, 

        // initialised to 0 (your specified default)

        $new_year = 0;


        // 3. check each year in your $array

        foreach ($array as $replacement_values){


            // 4. if $replacement_values has a YEAR and VALUE_AMDON...

            //    and $price_years_value matches the value in YEAR...

            if( $replacement_values['YEAR'] && $replacement_values['VALUE_AMDON']

                && $replacement_values['YEAR'] == $price_years_value){


                    // 5. store this in our $new_year variable

                    $new_year = $replacement_values['VALUE_AMDON'];

             }

        }

        

        // 6. after checking all years in $array, it we found a match it will be in $new_year

        // otherwise it will still be 0. 

        // Now simple set this value  - we can access this directly using $key and $index

        $priceYear[$key][$index] = $new_year;

    }

}

var_dump($priceYear);

使用不存在的年份的示例數(shù)據(jù):


$priceYear = array(

    'AZ' => array( 1 => 2021, 2 => 2020), // <-- 2021 doesn't exist in $array

    'BY' => array( 0 => 2020, 1 => 2016), // <-- 2016 doesn't exist in $array

    'CX' => array(1 => 2020, 2 => 2020, 3 => 2020),

    'DW' => array(106 => 2019, 107 => 2019, 108 => 2019)

);


$array = array(

    array('YEAR' => 2018, 'VALUE_AMDON' => 55),

    array('YEAR' => 2019, 'VALUE_AMDON' => 57),

    array('YEAR' => 2020, 'VALUE_AMDON' => 59)

);

輸出:


Array

(

    [AZ] => Array

        (

            [1] => 0       // <-- value is 0 because year didn't exist in $array

            [2] => 59

        )

    [BY] => Array

        (

            [0] => 59

            [1] => 0       // <-- value is 0 because year didn't exist in $array

        ) 

    [CX] => Array

        (

            [1] => 59

            [2] => 59

            [3] => 59

        )

    [DW] => Array

        (

            [106] => 57

            [107] => 57

            [108] => 57

        )

)


查看完整回答
反對 回復 2023-07-15
  • 2 回答
  • 0 關注
  • 237 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號