仅使用foreach循环在二维数组中打印特定文本

时间:2011-04-10 20:14:23

标签: php

给定的数组是

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

输出应如下所示 -

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 个答案:

答案 0 :(得分: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]);
}