duanji2002 2014-11-29 18:14
浏览 39

更改多维数组php的值

Its my first time working with multidimensional arrays in php. I need to change the second number in each sub array.

What I want is to check if the Id in the array matches the Id from the database. When the two match I want to change the 2nd entry in the sub array by adding a number to it. If the Id from the query does not match anything in the list I want a new sub array to be pushed to the end of the array with the values of Id and points_description.

Also, if its helpful, my program right now does find the matches. The only thing is, it does not update the 2D array.

$array = array(array());
while ($row_description = mysqli_fetch_array($query_description)) {
    $check = 1;
    $is_match = 0;

    foreach ($array as $i) {    
        foreach ($i as $value) {  
            if ($check == 1) {
                if ($row_description['Id'] == $value) {
                    //$array[$i] += $points_description;
                    $is_match = 1;
                }
             }

            $check++;
            $check %= 2; //toggle between check and points
        }
    }

    if ($is_match == 0) {
        array_push($array, array($row_description['Id'], $points_description));
    }
}

I feel like Im doing this so wrong. I just want to go through my 2D array and change every second value. The expected output should be a print out of all the Ids and their corresponding point value I hope this is helpful enough.

Example: $row_description['Id'] = 2 and $array = array(array(2,1), array(5,1) , array(6,1))

output should be $array = array(array(2,4), array(5,1) , array(6,1))

if $row_description['Id'] = 3 and $array = array(array(2,1), array(5,1) , array(6,1))

output should be $array = array(array(2,4), array(5,1) , array(6,1),array(3,3))

  • 写回答

1条回答 默认 最新

  • duanfei9278 2014-11-30 21:15
    关注

    By default PHP will copy an array when you use it in a foreach. To prevent PHP from creating this copy you need to use to reference the value with &

    Simple example :

    <?php
    
    $arrFoo = [1, 2, 3, 4, 5,];
    $arrBar = [3, 6, 9,];
    

    Default PHP behavior : Make a copy

    foreach($arrFoo as $value_foo) {
        foreach($arrBar as $value_bar) {
            $value_foo *= $value_bar;
        }   
    }
    
    
    var_dump($arrFoo);
    /* Output :
    array(5) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
      [3]=>
      int(4)
      [4]=>
      int(5)
    
    }
    */
    

    ByReference : Don't create the copy :

    foreach($arrFoo as &$value_foo) {
        foreach($arrBar as $value_bar) {
            $value_foo *= $value_bar;
        }   
    }
    
    var_dump($arrFoo);
    /* Output :
    array(5) {
      [0]=>
      int(162)
      [1]=>
      int(324)
      [2]=>
      int(486)
      [3]=>
      int(648)
      [4]=>
      &int(810)
    }
    
    */
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题