我希望有人可以帮助我。我阅读了很多关于数组的内容,并尝试了许多找到的例子,但我无法让我的代码工作。
首先我创建我的数组:
$aryUsers= array (
'users' =>
array ()
);
我从我的数据库中获取了一些数据,我将这样保存到我的数组中:
$user = array("ID" => $ID, "User" => $User, "val" => $val);
$aryUsers["users"][] = $user;
如果我现在 print_r 我的数组,它看起来像这样:
Array ( [users] => Array
(
[0] => Array ( [ID] => 696 [User] => user 1 [val] => 341 )
[1] => Array ( [ID] => 708 [User] => user 2 [val] => 218 )
[2] => Array ( [ID] => 670 [User] => user 3 [val] => 73 )
[3] => Array ( [ID] => 718 [User] => user 4 [val] => 66 )
[4] => Array ( [ID] => 698 [User] => user 5 [val] => 66 )
[5] => Array ( [ID] => 721 [User] => user 6 [val] => 60 )
[6] => Array ( [ID] => 707 [User] => user 7 [val] => 60 )
[7] => Array ( [ID] => 720 [User] => user 8 [val] => 141 )
)
)
如果我尝试使用此代码(或其他我尝试过)输出我的数组,那么我什么都没得到:
$iterations = count($aryUsers[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
foreach($aryUsers as $key => $value) {
echo $value[$i];
}
}
我做错了什么?或者我正在做一些正确的事情? 谢谢你的阅读!
答案 0 :(得分:2)
您好,给出了样本数据数据结构。
$arrayUsers = array (
'users' => array (
0 => array ( 'ID' => 696, 'User' => 'user 1', 'val' => 341 ),
1 => array ( 'ID' => 708, 'User' => 'user 2', 'val' => 218 ),
2 => array ( 'ID' => 670, 'User' => 'user 3', 'val' => 73 ),
3 => array ( 'ID' => 718, 'User' => 'user 4', 'val' => 66 ),
4 => array ( 'ID' => 698, 'User' => 'user 5', 'val' => 66 ),
5 => array ( 'ID' => 721, 'User' => 'user 6', 'val' => 60 ),
6 => array ( 'ID' => 707, 'User' => 'user 7', 'val' => 60 ),
7 => array ( 'ID' => 720, 'User' => 'user 8', 'val' => 141 ),
)
);
foreach ($arrayUsers as $arrayUser) {
foreach ($arrayUser as $arrayUser) {
echo $arrayUser['ID']. ' '. $arrayUser['User'] . ' ' . $arrayUser['val'].'<br>';
}
}