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)
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,