我有一个数组:
Array ( [results] => Array ( [0] => Array ( [username] => Jagan ) [1] =>Array ( [username] => rahul ) ) )
现在问题是都有名称为username
的索引,所以我不能简单地通过这样做来访问它们
<?php
foreach ($results as $result=>$res)
{
echo $res['username']);
}
?>
这将打印两个用户名,我想单独打印它们。我的数组概念有点弱。
答案 0 :(得分:0)
您尝试迭代的循环不会给您预期的结果。这将只循环一次。所以在$res
中你会得到这个
Array(0 => Array("username" => "Jagan"), 1 => Array("username" => "rahul"))
并且没有名称为username
将其更改为:
foreach ($results as $result=>$res)
:此:强>
foreach ($results["results"] as $result=>$res)
<强> PHPcode:强>
<?php
ini_set('display_errors', 1);
$results = Array("results" => Array(0 => Array("username" => "Jagan"), 1 => Array("username" => "rahul")));
foreach ($results["results"] as $result => $res)
{
echo $res['username'];
}