如何使用自定义键循环数组

时间:2014-07-18 15:36:36

标签: php arrays

如何使用自定义键循环数组:

$sql = "Select * from Users";
$result = mysql_query($sql, $db);

$rows = array();
while($list = mysql_fetch_assoc($result)) {
 $rows['customKey'] = array(0, 1, 2, 3);
 $rows[] = $list;
}

然后我把它传递给另一个数组

foreach($rows as $row) {
 $array = array (
  'test' => $row,
  'test2' => $row['userName'] 
)
}

然后我打印$ array

print_r($array['test']);

返回

Array ( [id] => 1 [userName] => John Smith ) 

为什么不在这里获得$ rows ['customKey']。

注意:我知道我需要停止使用mysql_ * ..这是一个遗留应用程序。

1 个答案:

答案 0 :(得分:0)

更改行:

$rows['customKey'] = array(0, 1, 2, 3);

为:

$list['customKey'] = array(0, 1, 2, 3);