1

I have a mutidimensional array:

 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

        )       

)

If i use foreach i get the output, but i would like to get the output grouped by the first key-Value or second (place_id or place_name) if they are the same. So basicly i woudl like to get the output like this:

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

If i use standard 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>'; 
            }

I gout output like this:

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

So NEW YORK (place_name) is duplicated. How can i merge or group by results based on that key-value?

Thanks, Misko

0

3 Answers 3

1

Some basic solution is tracking a place_name and see if it has changed:

// 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; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Future readers, if your subarrays are not ordered by place_name, call usort() to prepare the array before looping.
0

You need to add a new var with old place name, check if it == place_name from your array, if it doesn't echo place name, if it does echo connection only

Edit: This also means you will have to put it in order of place name, eg New York, New York, Dallas and not New York, Dallas, New York. But since your array is already set up that way you should be fine.

Comments

0

Complete solution using additional array of grouped "places"(by place_name key):

$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>';
}

The output(of course, in browser you'll see bolded/colored text):

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.