0
$useridtofind= 123;

$users=array();

while ($data = mysql_fetch_array ($result))
{

        $userid = $data['userid'];
    $age = $data['age'];
    $gender = $data['gender'];
    $dob = $data['dob'];

    $users[$userid] => array(
        'age'=> $age, 
        'gender'=> $gender, 
        'dob' => $dob
        )
}


$useridtofind=123;

for($v=0; $v< count($users); $v++)
{
    if($users[$v]== $useridtofind)
    {
        //how to go with grab value of age, gender, dob  here?      
    }
}
4

2 回答 2

2

您已经使用 id 来索引数组。

只需使用:

if (isset($users[$usertofind])) {
  $user = $users[$usertofind]; 
  echo $user['age'];
  echo $user['dob'];
  echo $user['gender'];
}

编辑:添加了 isset 检查并减少了引用相同数组元素的次数

于 2011-02-11T01:21:41.620 回答
0

您似乎使用用户 ID 作为用户在数组中的索引。如果是这种情况,您不应该使用for循环,您应该只检查数组中是否存在键:

$useridtofind = 123;

if (array_key_exists($useridtofind, $users)) {
  $user = $users[$useridtofind];

  echo "User exists: ", $user['age'], '/', $user['gender'], '/', $user['dob'];
} else {
  echo "User doesn't exist";
}
于 2011-02-11T01:21:31.383 回答