PHP foreach通过一个key->值循环遍历多维数组echo输出组

时间:2017-03-12 17:10:05

标签: php arrays multidimensional-array foreach

我有一个多维数组:

 Array
(
    [0] => Array
        (
            [place_id] => 2225
            [place_name] => NEW YORK
            [alarm] => PING
            [name_connection] => New-York-01
            [name_connection_id] => 11175
            [status] => 1
        )
    [1] => Array
        (
            [place_id] => 2225
            [place_name] => NEW YORK
            [alarm] => PING
            [name_connection] => New-York-03
            [name_connection_id] => 4324
            [status] => 1

        )   
    [2] => Array
        (
            [place_id] => 1345
            [place_name] => DALLAS
            [alarm] => PING
            [name_connection] => Dallas-03
            [name_connection_id] => 6666
            [status] => 1

        )       

)

如果我使用foreach,我得到输出,但我想得到的输出按第一个键值或第二个(place_id或place_name)分组(如果它们是相同的)。所以基本上我想得到这样的输出:

纽约
纽约01 | 11175 | 1
纽约03 | 4324 | 1
DALLAS
达拉斯-03 | 6666 | 1个

如果我使用标准foreach:

 foreach($place as $rowplace){

            $place_name3 = $rowplace['place_name'];
            $name_connection3 = $rowplace['name_connection'] ;

            echo' 
            <font color="green"> <b>'.$place_name3 .'</b><br> 
'.$name_connection3.'</font> <br>'; 
            }

我痛风输出如下:

纽约
纽约01 | 11175 | 1
纽约
纽约03 | 4324 | 1
DALLAS
达拉斯-03 | 6666 | 1个

所以NEW YORK(place_name)是重复的。如何基于该键值合并或按结果分组?

谢谢,Misko

3 个答案:

答案 0 :(得分:1)

一些基本解决方案是跟踪place_name并查看其是否已更改:

// variable to track previous place name
$prev_place_name = '';

foreach($place as $rowplace){
    $place_name3 = $rowplace['place_name'];

    // current place name differs from previous:
    if ($place_name3 != $prev_place_name) {
        echo '<font color="green"> <b>'.$place_name3 .'</b></font>';
        $prev_place_name = $place_name3;
    }

    $name_connection3 = $rowplace['name_connection'] ;
    echo '<br>'.$name_connection3; 
}

答案 1 :(得分:0)

你需要添加一个带有旧地名的新var,检查你的数组中是否== place_name,如果它没有回显地名,如果它只回显连接

编辑:这也意味着你必须按地名顺序排列,例如纽约,纽约,达拉斯,而不是纽约,达拉斯,纽约。但是因为你的数组已经设置好了,你应该没问题。

答案 2 :(得分:0)

使用其他数组的分组“地点”(按place_name键)完整解决方案:

$places = [
    [
        'place_id' =>  2225,
        'place_name' =>  'NEW YORK',
         'alarm' =>  'PING',
         'name_connection' =>  'New-York-01',
         'name_connection_id' =>  11175,
         'status' =>  1,
    ],
    [
        'place_id' =>  2225,
        'place_name' =>  'NEW YORK',
        'alarm' =>  'PING',
        'name_connection' =>  'New-York-03',
        'name_connection_id' =>  4324,
        'status' =>  1,
    ],
    [
        'place_id' =>  1345,
        'place_name' =>  'DALLAS',
        'alarm' =>  'PING',
        'name_connection' =>  'Dallas-03',
        'name_connection_id' =>  6666,
        'status' =>  1,
    ]
];

$grouped = [];
foreach ($places as $place) {
    // constructing the row with columns
    $line = implode("|", [$place['name_connection'], $place['name_connection_id'], $place['status']]);
    if (isset($grouped[$place['place_name']])) {
        $grouped[$place['place_name']] .= "</br>". $line;
    } else {
        $grouped[$place['place_name']] = $line;
    }
}

// outputting `places` data in tabular form
foreach ($grouped as $k => $columns) {
    echo '<font color="green"> <b>'. $k .'</b></br>'
            . $columns .'</font> </br>';
}

输出(当然,在浏览器中你会看到粗体/彩色文字):

 NEW YORK
New-York-01|11175|1
New-York-03|4324|1 
 DALLAS
Dallas-03|6666|1