Search code examples
phparraysmultidimensional-arraygroupingsub-array

Group multidimensional data by one column and create subarrays in each group


I have a page into PHP where I retrieve XML data from a server and I want to store this data into an array.

This is my code:

foreach ($xml->DATA as $entry){
    foreach ($entry->HOTEL_DATA as $entry2){
        $id = (string)$entry2->attributes()->HOTEL_CODE;
        $hotel_array2 = array();
        $hotel_array2['id'] = $entry2->ID;
            $hotel_array2['name'] = utf8_decode($entry2->HOTEL_NAME);
        $i=0;
        foreach($entry2->ROOM_DATA as $room){
            $room_array = array();
            $room_array['id'] = (string)$room->attributes()->CCHARGES_CODE;
            $hotel_array2['rooms'][$i] = array($room_array);

            $i++;
        }
            array_push($hotel_array, $hotel_array2);
        }
}

In this mode I have the array hotel_array which all hotel with rooms. The problem is that: into my XML I can have multiple hotel with same ID (the same hotel) with same information but different rooms. If I have an hotel that I have already inserted into my hotel_array I don't want to insert a new array inside it but I only want to take its rooms array and insert into the exisiting hotel. Example now my situation is that:

hotel_array{
      [0]{
           id = 1,
           name = 'test'
           rooms{
               id = 1
           }
      }
      [0]{
           id = 2,
           name = 'test2'
           rooms{
               id = 100
           }
      }
      [0]{
           id = 1,
           name = 'test'
           rooms{
               id = 30
           }
      }
}

I'd like to have this result instead:

hotel_array{
      [0]{
           id = 1,
           name = 'test'
           rooms{
               [0]{
                  id = 1
               }
               [1]{
                  id = 30
               }
           }
      }
      [0]{
           id = 2,
           name = 'test2'
           rooms{
               id = 100
           }
      }
}

How to create an array like this?


Solution

  • first thing is it helps to keep the hotel id as the index on hotel_array when your creating it.

    foreach ($xml->DATA as $entry){
        foreach ($entry->HOTEL_DATA as $entry2){
            $id = (string)$entry2->attributes()->HOTEL_CODE;
            $hotel_array2 = array();
            $hotel_array2['id'] = $entry2->ID;
                $hotel_array2['name'] = utf8_decode($entry2->HOTEL_NAME);
            $i=0;
            foreach($entry2->ROOM_DATA as $room){
                $room_array = array();
                $room_array['id'] = (string)$room->attributes()->CCHARGES_CODE;
                $hotel_array2['rooms'][$i] = array($room_array);
    
                $i++;
            }
                if (!isset($hotel_array[$hotel_array2['id']])) {
                      $hotel_array[$hotel_array2['id']] = $hotel_array2;
                } else {
                      $hotel_array[$hotel_array2['id']]['rooms'] = array_merge($hotel_array[$hotel_array2['id']]['rooms'], $hotel_array2['rooms']);
                }
            }
    }