将数组从mysql_fetch_array()复制到另一个数组

时间:2017-04-02 08:39:11

标签: php mysql

我有一个多维数组,它从mysql_fetch_array()函数中获取数据。但是当我尝试将它复制到另一个数组时,它只复制前一个数组的第一个索引。

这是我的代码片段:

$old = array();

$sql = "SELECT price, item FROM sale";
$query = mysql_query($sql, $con) or die("sql error : ".mysql_error());
while($data = mysql_fetch_array($query)){
    $old[] = $data;
}

$new = array();
foreach($old as $foo){
$new[] = $foo;
}

print_r($old);

将显示:

Array (
      [0] => Array ( [0] => 1100 [price] => 1100 [1] => 1 [item] => 1 ) 
      [1] => Array ( [0] => 1200 [price] => 1200 [1] => 10 [item] => 10 )
      [2] => Array ( [0] => 300 [price] => 300 [1] => 15 [item] => 15 )
) 

,而

print_r($new);

仅显示:

Array ([0] => Array ( [0] => 1100 [price] => 1100 [1] => 1 [item] => 1 ))

还有另一种方法来复制$ old数组的整个数据吗?

0 个答案:

没有答案