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

军团

我有以下数组

$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
                          )
             ) 

我就是这样

function replace($var){
    global $array;
    for ($u=0; $u <sizeof($array) ; $u++) { 
        if ($var == $array[$u]['YEAR']){
            return $array[$u]['VALUE_AMDON'];
        }else{
            return $var;
        }
    }
}

foreach ($priceYear as $key => $value) {
    $priceYear[$key] = array_map('replace', $value);
}

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

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

蓬松的小猫

因为数组的键之间没有直接链接,所以最简单的方法是使用嵌套循环。

你只需要遍历的$priceYear数组,检查每一个“年”值的acainst的YEAR价值$array如果它们匹配,则可以只替换$priceYear数组中的该值

注释下面的代码以告诉您每一行在做什么:

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

使用不存在的年份的示例数据:

$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
        )
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何根据Numpy中另一个数组的分组值生成一个数组?

用另一个数组中的值替换numpy数组的切片

从另一个数组中删除另一个数组中的值的最快方法

使用另一个数组中的值过滤一个数组

从另一个数组中删除一个数组中的值

根据另一个数组中的值获取键数组 JS

从另一个数据框中替换一个数据框中的值

如何转换一个数组中的首字母以匹配另一个数组中的值

根据javascript中的另一个数组更改对象数组的值

如何根据另一个数组中的重复值在一个数组中添加值?

如何用另一个数组替换多维数组中的某些值

根据另一个数组中的值删除嵌套对象数组中的值

如何从另一个数组中的一个数组中选择一个值?

根据另一个数组中的值对numpy数组的选择索引执行操作

用Julia中另一个数组的值仅替换一个数组中的零

如何用另一个数组索引值替换一个数组键值?

数组值替换为php中的另一个数组值

通过另一个数组中的值过滤一个数组?

用PHP中的另一个数组键替换一个数组键

根据另一个数据框替换一个数据框中的列中的多个值

用php中的另一个数组替换数组的值

打印一个数组中的 JSON 值,这个数组在另一个数组中

用另一个数组中的代码替换一个数组中的国家名称

如何根据Java中另一个数组的值对一个数组进行排序?

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

用另一个数组的相同索引的值替换一个数组中的值?

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

根据另一个数组中的值过滤对象数组

根据另一个数组中的值从数组中删除对象