Php - 添加到Array失败,引用添加

时间:2013-06-13 22:17:40

标签: php arrays reference add

我的php环境有一些奇怪的问题:

以下代码:

public static function getAllVotesOfGroup($groupid){
    $con = new \SYSTEM\DB\Connection(new \DBD\uVote());
    $res = $con->prepare(   'selVoteByGrp',
                            'SELECT * FROM `uvote_votes` WHERE `group` = ?;',
                            array($groupid));
    $result = array();

    //$r = array();
    while($r = $res->next()){                        
        //print_r($r);                        
        $result[] = $r;
        print_r($result);            
        echo "</br></br>";
    }           
    //print_r($result);
    return $result;
}


public function next($object = false, $result_type = MYSQL_BOTH){        
    if($object){
        $this->current = mysqli_fetch_object($this->res);
    } else {
        $this->current = mysqli_fetch_assoc($this->res);
    }
    return $this->current;
}

在我的其他机器上,此代码返回我想要的所有值,此机器返回: - &gt;来自getAllVotesOfGroup(1)的回显代码

Array ( [0] => Array ( [ID] => 1 [group] => 1 [title] => Test [text] => Testabstimmung [time_start] => 2013-06-12 [time_end] => 2015-06-13 ) ) 

Array ( [0] => Array ( [ID] => 2 [group] => 1 [title] => Test2 [text] => Testabstimmung [time_start] => 2014-06-13 [time_end] => 2012-06-16 ) [1] => Array ( [ID] => 2 [group] => 1 [title] => Test2 [text] => Testabstimmung [time_start] => 2014-06-13 [time_end] => 2012-06-16 ) ) 

Array ( [0] => Array ( [ID] => 3 [group] => 1 [title] => Test3 [text] => bla [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [1] => Array ( [ID] => 3 [group] => 1 [title] => Test3 [text] => bla [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [2] => Array ( [ID] => 3 [group] => 1 [title] => Test3 [text] => bla [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) ) 

Array ( [0] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [1] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [2] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [3] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) ) 

正如您所看到的那样,之前的值被替换了,因为$ r是一个引用,只有引用被添加到数组而不是实际值。

为什么?我可以设置一些php.ini选项来改变这种行为吗?

增加: 这很好用!但这不是我想要的; - )

    $result = array();

    while($r = $res->next()){                        
        $result[] = array('title' => $r['title'],'text' => $r['text']);
    }           
    return $result;

好的我可以解决它; - )

请参阅http://www.php.net/manual/en/mysqli-stmt.fetch.php#83284

问题是mysqli-stmt-fetch im使用它将返回引用而不是数据。

1 个答案:

答案 0 :(得分:0)

就我所见,代码工作得很好(除非我遗漏了什么)。

$ result [] = $ r;

这一行是将新行添加到数组中而不是写入旧值。说实话,我会质疑其他服务器为你返回的内容。