douluolan9101 2013-06-26 18:41
浏览 35
已采纳

如何正确替换数组中的value元素?

I want to compare the key values ​​in the array and if a match is found to increase the value of the element.

For this I use code:

// var_dump $all_array 
array(2) { 
    [0]=> array(6) { 
        [0]=> string(8) "art_7880"
        [1]=> string(11) "Арт.7880"
        [2]=> string(1) "1"
        [3]=> NULL
        [4]=> string(45) "png"
        [5]=> int(1372269755) 
    } 
    [1]=> array(6) { 
         [0]=> string(8) "art_7880"
         [1]=> string(11) "Арт.7880"
         [2]=> string(1) "1"
         [3]=> NULL
         [4]=> string(45) "png"
         [5]=> int(1372269874) 
    } 
}
// var_dump $count 
array(2) { [0]=> string(2) "10" [1]=> string(1) "1" }

// var_dump $product
array(2) { [0]=> string(10) "1372269755" [1]=> string(10) "1372269874" }

$count=$_POST['count'];
$product=$_POST['product'];
$count_arr_products=count($product);

for ($i=0; $i<=$count_arr_products; $i++){    
    foreach ($all_array as $keys => $elms) {
        if ($product[$i]==$elms[5]) {
            if($count[$i] > 0) {
                $elms[2] = $count[$i];
            } else {
                unset($keys);
            }
        }
    }
}

but step $elms[2] = $count[$i]; not work - in result value $elms[2] not change...

  • 写回答

1条回答 默认 最新

  • dongxing2015 2013-06-26 18:48
    关注

    You need to majke $elms a reference. By default it will be a copy of the sub-array, so assignment won't update the original array.

    $all_array = array(array("art_7880", "Арт.7880", "1", NULL, "png", 1372269755),
                       array("art_7880", "Арт.7880", "1", NULL, "png", 1372269874));
    $count = array("10", "1");
    $product = array("1372269755", "1372269874");
    
    $count = array("10", "1");
    $product = array("1372269755", "1372269874");
    $count_arr_products = count($product);
    for($i=0; $i<$count_arr_products; $i++){ // Use < not <=
      foreach ($all_array as $keys => &$elms) { // Use a reference so we can update it
        if ($product[$i]==$elms[5]){
          if ($count[$i] > 0) {
            $elms[2] = $count[$i];
          } else {
            unset($all_array[$keys]); // not unset($keys)
          }
        }
      }
    }
    var_dump($all_array);
    

    Output:

    array(2) {
      [0]=>
      array(6) {
        [0]=>
        string(8) "art_7880"
        [1]=>
        string(11) "Арт.7880"
        [2]=>
        string(2) "10"
        [3]=>
        NULL
        [4]=>
        string(3) "png"
        [5]=>
        int(1372269755)
      }
      [1]=>
      &array(6) {
        [0]=>
        string(8) "art_7880"
        [1]=>
        string(11) "Арт.7880"
        [2]=>
        string(1) "1"
        [3]=>
        NULL
        [4]=>
        string(3) "png"
        [5]=>
        int(1372269874)
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来