donglu5041 2011-01-16 21:34
浏览 38

关于操纵“mysql_fetch_array”结果的问题

By default mysql_fetch_array returns numeric and associative array togeather. This function is useful for reading, while you can get values

either by statement : echo arr[1]; //circle or echo arr["shape"]; //circle

I am new to PHP and I run into the troubles when I wanted to change the values of that fetched array: arr[1] = square;

I supposed that arr["shape"] will be 'square' too, but I was wrong. arr["shape"] still remains 'circle'. Obviously you have to change both values to 'circle'.

Moreover: I wrote a funcion which should return changed shape array, (which is 2D array). I got different outcomes, if I used the first or

the second line of code.

function ChangeShape($arr)
        {

            for ($i=0; $i<$count($arr); $i++)
            {
         1.    $arr[$i]["shape"] = 'square'; // fail to return changed $arr - it is (probably) reference to number counterpart

         2.    $arr[$i][2] = 'square';       // this WILL return changed $arr
            }
            }
            return $arr;
        }

My questions: 1. is there any technique in PHP by which I can change both values at once? 2. in my app I use assoc nicknames to access array values. But when it is not possible to change numeric alias at the same time, what is

the easiest way to do it?

thank for reading this to the end... hope you answer.

Thanks a lot.

  • 写回答

3条回答 默认 最新

  • drkug66408 2011-01-16 22:04
    关注

    If you just want the data in a simple array once you grab it, you can always just simplify it by putting it in a few array:

     $query = "SELECT * from myTable";
     $result = mysql_query($query) or die(mysql_error());
     $newArray = array();
    
     $i=0; 
     while($row = mysql_fetch_array($result)){
       array_push(  $newArray, $row[$i]['name'] );
     }
     print_r($newArray);
     $newArray[4] = "some other value than what came from the db";
     print_r($newArray);
    

    It doesn't seem super clear why your data is coming back as complicated as it seems to be, but this might help clear it up.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集