需要帮助php中的多个foreach语句

时间:2011-11-03 15:54:41

标签: php foreach

请告诉我如何为下面的数组编写foreach循环:

Array
(
    [0] => Array
        (
            [id] => 1
            [sector] => Agriculture
            [experience] => 2
            [first_name] => ranjith
            [second_name] => 
            [position] => 
            [telephone] => 442254322
            [mobile] => 999999999
            [email] => dfd@gmail.com
            [education] => Phd
            [resume] => 
            [photograph] => 
            [comments] => goodsdsdfdfd
            [status] => 1
        )

    [1] => Array
        (
            [id] => 2
            [sector] => Water
            [experience] => 3
            [first_name] => jagdish
            [second_name] => 
            [position] => 
            [telephone] => 99999
            [mobile] => 99
            [email] => jag@gmail.com
            [education] => sdsdf
            [resume] => 
            [photograph] => 
            [comments] => sdfsdf
            [status] => 0
        )
     )

5 个答案:

答案 0 :(得分:4)

foreach($arr as $item)
{
    foreach($item as $key => $value)
    {
        if($key == 'first_name')
        {
            echo $value;
        }
    }
}

答案 1 :(得分:1)

如果您只是寻找第一个和第二个名称值,我假设填充一个全名

foreach ($arr as $item) 
{
    $name = $item['first_name'];
    $name .= $item['second_name'] != '' ? ' '.$item['second_name'] : '';
    echo $name;
}

答案 2 :(得分:0)

foreach($array as $k=>$v)
    foreach($v as $kk=>$vv)
        echo "$kk for $k is $vv\n";

答案 3 :(得分:0)

要输出吗?

foreach($data as $record) {
    foreach($record as $k => $v) {
        echo $k.":".$v
    }
}

答案 4 :(得分:0)

foreach($arr as $item)
    foreach($item as $key => $value)
    {
        if($key == 'first_name')
            echo $value; // print the firstname
        elseif($key == 'second_name')
            echo $value; // print the second name
    }