2011-04-10 83 views
0

給定陣列中的2- d陣列的特定文本是打印使用foreach循環僅

$info=array("sanjay"=>array("male",20), 
      "ravi"=>array("male",20), 
      "rahul;"=>array("male",26), 
      "tina"=>array("male",21), 
      "sanjna"=>array("female",60) 
      ); 

輸出應該看起來像this--

Name is: Sanjay Sex is: male and Age is: 20 
Name is: ravi Sex is: male and Age is: 20 
Name is: rahul Sex is: male and Age is: 26 
Name is: tina y Sex is: female and Age is: 21 
Name is: Sanjna Sex is: female and Age is: 60 

回答

1

使用foreach環路將是在此情況下的最佳:

foreach($info as $name => $more_info) 
{ 
    printf("Name is: %s Sex is: %s and Age is: %d<br />", $name, $more_info[0], $more_info[1]); 
} 
+0

是的,這工作得很好..很多蒂姆:) – swapnesh 2011-04-10 20:17:48