【问题标题】:How do I format following array/object如何格式化以下数组/对象
【发布时间】:2015-01-18 11:41:15
【问题描述】:

我通过调用 api 获取数组/对象

如果房间类型更多,我会得到这样的数组

 [HotelRoomResponse] => Array
                (
                    [0] => stdClass Object
                        (
                            [rateCode] => 203735593
                            [rateDescription] => Mango Standard
                            [RoomType] => stdClass Object
                                (
                                    [@roomTypeId] => 766936
                                    [@roomCode] => 200163941
                                    [description] => Mango Standard
                                    [descriptionLong] => <strong><ul><li>One Twin Bed</li></ul></strong>This city view room measures 247 square feet (23 square meters). Complimentary wireless Internet access keeps you connected, and the 26-inch flat-screen TV offers cable channels. A coffee/tea maker is provided. The private bathroom has a shower with a rainfall showerhead, as well as complimentary toiletries. Climate control, air conditioning, and a ceiling fan are among the conveniences offered.  <p></p>
                                 )
                           )
                      [1] => stdClass Object
                        (
                        [rateCode] => 200928482
                        [rateDescription] => Mango Standard
                        [RoomType] => stdClass Object
                            (
                                [@roomTypeId] => 766936
                                [@roomCode] => 200163941
                                [description] => Mango Standard
                                [descriptionLong] => <strong><ul><li>One Twin Bed</li></ul></strong>This city view room measures 247 square feet (23 square meters). Complimentary wireless Internet access keeps you connected, and the 26-inch flat-screen TV offers cable channels. A coffee/tea maker is provided. The private bathroom has a shower with a rainfall showerhead, as well as complimentary toiletries. Climate control, air conditioning, and a ceiling fan are among the conveniences offered.  <p></p>
                             )
                        )

                )

如果房间类型只有一种,我会得到关注对象

  [HotelRoomResponse] => stdClass Object
                    (
                        [rateCode] => 1273814
                        [rateDescription] => Deluxe Double Room
                        [RoomType] => stdClass Object
                            (
                                [@roomTypeId] => 488629
                                [@roomCode] => 379721
                                [description] => Deluxe Double Room
                                [descriptionLong] => <strong><ul><li>2 beds</li></ul></strong>
                              )
                      )

我想将第二个对象序列化为第一个对象,即如果我们获得更多房间类型

我怎样才能让上面的对象喜欢这个

 [HotelRoomResponse] => Array
                  (
                     [0] =>stdClass Object
                       (
                        [rateCode] => 1273814
                        [rateDescription] => Deluxe Double Room
                        [RoomType] => stdClass Object
                            (
                                [@roomTypeId] => 488629
                                [@roomCode] => 379721
                                [description] => Deluxe Double Room
                                [descriptionLong] => <strong><ul><li>2 beds</li></ul></strong>
                              )
                      )

                   ) 

也在这里检查问题http://developer.ean.com/docs/error-handling/special-cases/axis-net-json-issues/ 我没有任何 PHP 解决方案。 谁能帮帮我。

【问题讨论】:

  • 写代码将是一个好的开始...
  • 使用is_arrayis_object 来检查你想对你得到的数据做什么并采取相应的行动
  • 如何在 API 中构建响应?
  • 对于多个它给出数组。如果只有一个,则显示单个对象。如何编写循环以适应这两种情况。
  • 你也可以在这里查看问题http://developer.ean.com/docs/error-handling/special-cases/axis-net-json-issues/

标签: php arrays object multidimensional-array


【解决方案1】:

这是我的优化解决方案

if(is_object($HotelRoomResponse)) {
    $NewHotelRoomResponse[0] = $HotelRoomResponse;
    $HotelRoomResponse = $NewHotelRoomResponse;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2020-08-10
    • 2023-03-08
    • 2019-10-14
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多