1

我有一个生成数组的函数,所以我可以进一步这样做:

while ($this->stmt->fetch()) // $row is generated here
{
   .....
}

如果我在这个while循环中编写print_r($row)它可以正常显示不同的数组

但是..如果我在那里写,$return[] = $row那么$return数组里面有所有重复的条目

这怎么可能?我非常筋疲力尽,无法弄清楚。

更新

public function bind_array($stmt, &$row)
{
      $md = $stmt->result_metadata();

      $params = array();
      while($field = $md->fetch_field()) {
         $params[] = &$row[$field->name];   
      }

   call_user_func_array(array($stmt, 'bind_result'), $params);
}

public function results($method = 'assoc', $close_stmt = false)
{
   $this->bind_array($this->stmt, $row);

   while ($this->stmt->fetch())
   {
      $ret[] = $row; // if write here print_r($row) instead everything is good
   }
   print_r($ret); // all are duplicate, 'id' are same, etc..
}
4

2 回答 2

0

我不知道怎么解决的,但我是这样解决的:

while ($this->stmt->fetch())
{
    foreach ($row as $k => $v)
    {
        $r[$k] = $v;
    }
    $ret[] = $r;
}

print_r($ret); // It gave different results, finally!
于 2013-01-24T22:35:08.787 回答
0

只是一个想法,我认为更简单(用数组填充数组);

// $row = array("foo" => "the foo");
foreach ($row as $k => $v) $ret[] = array($k => $v);

给;

Array
(
    [0] => Array
        (
            [foo] => the foo
        )

)
于 2013-01-24T23:47:48.440 回答