1

大家好,我有一个 PHP 页面,我从服务器检索 XML 数据,我想将这些数据存储到一个数组中。

这是我的代码:

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);
        }
}

在这种模式下,我有数组hotel_array,所有酒店都有房间。问题是:在我的 XML 中,我可以拥有多个具有相同 ID(同一家酒店)且信息相同但房间不同的酒店。如果我有一个已经插入到我的hotel_array 中的酒店,我不想在其中插入一个新数组,但我只想获取它的房间数组并插入到现有的酒店中。现在的例子我的情况是:

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
           }
      }
}

我想要这个结果:

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

如何创建这样的数组?

谢谢

4

3 回答 3

2

第一件事是它有助于将酒店 ID 作为hotel_array创建时的索引。

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']);
            }
        }
}
于 2013-06-27T07:00:43.113 回答
1

虽然这与 DevZer0 (+1) 的答案相似,但也可以做很多事情来简化您的工作……不需要使用array_merge一个,或者$i在您的房间阵列中具体说明。

$hotels = array();
foreach ($xml->DATA as $entry){
  foreach ($entry->HOTEL_DATA as $entry2){
    $id = (string) $entry2->attributes()->HOTEL_CODE;
    if ( empty($hotels[$id]) ) {
      $hotels[$id] = array(
        'id' => $id,
        'name' => utf8_decode($entry2->HOTEL_NAME),
        'rooms' => array(),
      );
    }
    foreach($entry2->ROOM_DATA as $room){
      $hotels[$id]['rooms'][] = array(
        'id' => (string) $room->attributes()->CCHARGES_CODE;
      );
    }
  }
}

以防万一它有帮助...

于 2013-06-27T07:09:47.057 回答
0

和这个 :)

$hotel_array = array();

foreach ($xml->DATA as $entry)
{
    foreach ($entry->HOTEL_DATA as $entry2)
    {
        $hotel_code = (string) $entry2->attributes()->HOTEL_CODE;

        if (false === isset($hotel_array[$hotel_code]))
        {
            $hotel = array(
                'id' => $entry2->ID,
                'code' => $hotel_code,
                'name' => utf8_decode($entry2->HOTEL_NAME)
            );

            foreach($entry2->ROOM_DATA as $room)
            {
                $hotel['rooms'][] = array(
                    'id' => (string)$room->attributes()->CCHARGES_CODE,
                );
            }

            $hotel_array[$hotel_code] = $hotel;
        }
    }
} 
于 2013-06-27T07:15:27.770 回答