2009-12-12 104 views
2

如何复制其中有其他关联数组的数组? 我正在讨论从mysql_fetch_assoc返回的结果集。创建一个结果行的副本

所以说,我有一个这样的结构...

connect 
$result = query; 

while ($row = mysql_fetch_assoc($result)) { 

    array_push($static_row, $row); // here lies the problem 

} 

我想获得的是$static_row完全一样的$row副本。最后,我想提出的一个功能,查询和while循环,并简单地返回$static_row

作为参考,$row一个print_r看起来像这样

Array ([key1] => value1 [key2] => value2) 
Array ([key1] => value1 [key2] => value1) 

谢谢, 让我知道如果你需要更多细节

回答

3

使用形式:

connect $result = query; 
while ($row = mysql_fetch_assoc($result)) 
{ 
    $rows[] = $row; 
} 
// now you have all the answers in an array of arrays, which you can return from a function 
+0

+1这比'array_push'更好。 – Franz 2009-12-12 11:19:38

1

嗯,我不完全确定你要做什么,但它看起来像你有复制分配(应该像这样工作)在循环内。也许这是你的问题。

+0

哦,我...对不起...做了一个小布布有 这是while循环中发生的事情... array_push($ static_row,$ row); 让我编辑上面的东西 – abhishekbh 2009-12-12 03:17:32

+0

@unknown,然后编辑你的问题。 – Don 2009-12-12 03:19:48

+0

你知道什么... nvm ...我正在犯一个非常愚蠢的错误,我刚刚修复了... 它的工作原理应该是 – abhishekbh 2009-12-12 03:21:35

-1

php doc page

// Fetching all the results to array with one liner: 
$result = mysql_query(...); 
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray))