根据一个数组中的值替换另一个数组中的值

我有以下数组


$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

             )

      ) 

还有另一个数组


$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

                          )

             ) 



但不幸的是它不起作用,它返回初始数组


如果有人可以帮助我,看起来这个错误非常愚蠢,但我没有看到它:c


哈士奇WWW
浏览 200回答 2
2回答

紫衣仙女

您还可以使用数组函数(例如 array_column 和 array_search 检查如下)来完成此操作:$array_year = array_column($array, "YEAR");foreach($priceYear as $key => $val){&nbsp; &nbsp; foreach($val as $innerkey => $innerval){&nbsp; &nbsp; &nbsp; &nbsp; // Below If year exist in $array_year function return key of $array_year else return false&nbsp; &nbsp; &nbsp; &nbsp; $isExistKey = array_search($innerval, $array_year);&nbsp; &nbsp; &nbsp; &nbsp; if($isExistKey !== FALSE){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $priceYear[ $key ][ $innerkey ] = $array[ $isExistKey ]["VALUE_AMDON"];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}echo "<pre>";print_r($priceYear);在此处检查输出:https://paiza.io/projects/gUNihGow6-CWO0eqWpEtxg ?language=php

杨__羊羊

由于数组的键之间没有直接链接,因此最简单的方法是使用嵌套循环。您只需要循环遍历$priceYear数组并检查每个“年份”值和YEAR中的值$array。如果它们匹配,您可以仅替换$priceYear数组中的该值。下面的代码带有注释,可以告诉您每一行的作用:// 1. loop through the nested arrays in $priceYearforeach ($priceYear as $key => $price_years_array) {&nbsp; &nbsp; foreach ($price_years_array as $index => $price_years_value) {&nbsp; &nbsp; &nbsp; &nbsp; // 2. Create a variable to store the new value for the year in this $priceYear array,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // initialised to 0 (your specified default)&nbsp; &nbsp; &nbsp; &nbsp; $new_year = 0;&nbsp; &nbsp; &nbsp; &nbsp; // 3. check each year in your $array&nbsp; &nbsp; &nbsp; &nbsp; foreach ($array as $replacement_values){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 4. if $replacement_values has a YEAR and VALUE_AMDON...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; and $price_years_value matches the value in YEAR...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $replacement_values['YEAR'] && $replacement_values['VALUE_AMDON']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && $replacement_values['YEAR'] == $price_years_value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 5. store this in our $new_year variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_year = $replacement_values['VALUE_AMDON'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // 6. after checking all years in $array, it we found a match it will be in $new_year&nbsp; &nbsp; &nbsp; &nbsp; // otherwise it will still be 0.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Now simple set this value&nbsp; - we can access this directly using $key and $index&nbsp; &nbsp; &nbsp; &nbsp; $priceYear[$key][$index] = $new_year;&nbsp; &nbsp; }}var_dump($priceYear);使用不存在的年份的示例数据:$priceYear = array(&nbsp; &nbsp; 'AZ' => array( 1 => 2021, 2 => 2020), // <-- 2021 doesn't exist in $array&nbsp; &nbsp; 'BY' => array( 0 => 2020, 1 => 2016), // <-- 2016 doesn't exist in $array&nbsp; &nbsp; 'CX' => array(1 => 2020, 2 => 2020, 3 => 2020),&nbsp; &nbsp; 'DW' => array(106 => 2019, 107 => 2019, 108 => 2019));$array = array(&nbsp; &nbsp; array('YEAR' => 2018, 'VALUE_AMDON' => 55),&nbsp; &nbsp; array('YEAR' => 2019, 'VALUE_AMDON' => 57),&nbsp; &nbsp; array('YEAR' => 2020, 'VALUE_AMDON' => 59));输出:Array(&nbsp; &nbsp; [AZ] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 0&nbsp; &nbsp; &nbsp; &nbsp;// <-- value is 0 because year didn't exist in $array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 59&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [BY] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 59&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 0&nbsp; &nbsp; &nbsp; &nbsp;// <-- value is 0 because year didn't exist in $array&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp;&nbsp; &nbsp; [CX] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 59&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 59&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 59&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [DW] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [106] => 57&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [107] => 57&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [108] => 57&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP