2011-02-11 76 views
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?  
    } 
} 

回答

0

您似乎将用户标识用作数组中用户的索引。如果是这样的话,你不应该使用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"; 
} 
2

你已经使用id索引数组。

只需使用:

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

EDIT:添加isset检查和减少的次引用相同的数组元素