将数据从simpleXML重新调整为数组

时间:2012-01-28 15:05:25

标签: php arrays object simplexml

我无法从对象访问变量。该对象来自API Feed,var_dump如下所示:

object(stdClass)#2 (1) {
  ["GetBookingsResult"]=>
  string(189227) "<Bookings>
  <Data>
    <BookingDate>2012-01-06T00:00:00-06:00</BookingDate>
    <StartBookingDate>2012-01-06T00:00:00-06:00</StartBookingDate>
    <RoomDescription>Dorm A</RoomDescription>
    <TimeEventStart>2012-01-06T15:00:00-06:00</TimeEventStart>
    <TimeEventEnd>2012-01-07T00:00:00-06:00</TimeEventEnd>
    <GroupName>Family Retreat</GroupName>
    <EventName>Family Retreat</EventName>
    <SetupTypeDescription>(none)</SetupTypeDescription>
    <SetupCount>27</SetupCount>
    <ReservationID>1222</ReservationID>
    <EventCoordinator />
    <GroupID>12</GroupID>
    <VIP xml:space="preserve"> </VIP>
    <VIPEvent>false</VIPEvent>
    <ClosedAllDay>false</ClosedAllDay>
    <OpenTime>1900-01-01T00:00:00-06:00</OpenTime>
    <CloseTime>1900-01-01T00:00:00-06:00</CloseTime>
    <GroupTypeDescription>Religious - Conference</GroupTypeDescription>
    <EventTypeDescription>Retreat</EventTypeDescription>
    <Contact>Phyllis 0000000000</Contact>
    <AltContact />
    <BookingID>29512</BookingID>
    <TimeBookingStart>2012-01-06T15:00:00-06:00</TimeBookingStart>
    <TimeBookingEnd>2012-01-07T00:00:00-06:00</TimeBookingEnd>
    <GMTStartTime>2012-01-06T15:00:00-06:00</GMTStartTime>
    <GMTEndTime>2012-01-07T00:00:00-06:00</GMTEndTime>
    <BuildingCode>Asb</BuildingCode>
    <Building>Dormitory</Building>
    <RoomCode>ASB A</RoomCode>
    <Room>Dorm A</Room>
    <RoomID>181</RoomID>
    <BuildingID>20</BuildingID>
    <StatusID>1</StatusID>
    <StatusTypeID>-14</StatusTypeID>
    <EventTypeID>1</EventTypeID>
    <GroupTypeID>1</GroupTypeID>
    <DateAdded>2011-01-09T08:25:02.72-06:00</DateAdded>
    <AddedBy>B Jordan</AddedBy>
    <DateChanged>2012-01-08T08:34:23.2-06:00</DateChanged>
    <ChangedBy>J Smith</ChangedBy>
  </Data>

  <Data>
    <BookingDate>2012-01-06T00:00:00-06:00</BookingDate>
    <StartBookingDate>2012-01-06T00:00:00-06:00</StartBookingDate>
    <RoomDescription>Dorm A1</RoomDescription>
    <TimeEventStart>2012-01-06T15:00:00-06:00</TimeEventStart>
    <TimeEventEnd>2012-01-07T00:00:00-06:00</TimeEventEnd>
    <GroupName>Family Retreat</GroupName>
    <EventName>Family Retreat</EventName>
    <SetupTypeDescription>(none)</SetupTypeDescription>
    <SetupCount>27</SetupCount>
    <ReservationID>1222</ReservationID>
    <EventCoordinator />
    <GroupID>12</GroupID>
    <VIP xml:space="preserve"> </VIP>
    <VIPEvent>false</VIPEvent>
    <ClosedAllDay>false</ClosedAllDay>
    <OpenTime>1900-01-01T00:00:00-06:00</OpenTime>
    <CloseTime>1900-01-01T00:00:00-06:00</CloseTime>
    <GroupTypeDescription>Religious - Conference</GroupTypeDescription>
    <EventTypeDescription>Retreat</EventTypeDescription>
    <Contact>Phyllis 0000000000</Contact>
    <AltContact />
    <BookingID>29512</BookingID>
    <TimeBookingStart>2012-01-06T15:00:00-06:00</TimeBookingStart>
    <TimeBookingEnd>2012-01-07T00:00:00-06:00</TimeBookingEnd>
    <GMTStartTime>2012-01-06T15:00:00-06:00</GMTStartTime>
    <GMTEndTime>2012-01-07T00:00:00-06:00</GMTEndTime>
    <BuildingCode>Asb</BuildingCode>
    <Building>Dormitory</Building>
    <RoomCode>Asb A1</RoomCode>
    <Room>Dorm A1</Room>
    <RoomID>7</RoomID>
    <BuildingID>20</BuildingID>
    <StatusID>1</StatusID>
    <StatusTypeID>-14</StatusTypeID>
    <EventTypeID>1</EventTypeID>
    <GroupTypeID>1</GroupTypeID>
    <DateAdded>2011-01-09T08:25:02.72-06:00</DateAdded>
    <AddedBy>P Jordan</AddedBy>
    <DateChanged>2012-01-08T08:34:23.2-06:00</DateChanged>
    <ChangedBy>J Smith</ChangedBy>
  </Data>
</Bookings>"
}

我希望能够重新排序返回到数组中的信息,以便我可以按日期对事件进行分组,然后按EventName对它们进行排序。以下是我的代码:

$xml = simplexml_load_string($result->GetBookingsResult);

$list='';
    foreach($xml as $event){
        $st_date = lvmDateFormat($event->TimeBookingStart,'name');//function reformats date
        $en_date = lvmDateFormat($event->TimeBookingEnd,'name');
        $name =  lvmDateFormat($event->TimeBookingStart,'cal');
        $ename = $event->EventName;
$events[lvmDateFormat($event->TimeBookingStart,'name')][$st_date] = array(
            'EventName' => $ename
            ,'Date' => $name
            ,'TimeEventStart' => $st_date
            ,'TimeEventEnd' => $en_date
        );
    }

如果我回显$event->EventName;它看起来很好,但当我尝试将它添加到数组时,它会像这样回来:

 [EventName] => SimpleXMLElement Object
                (
                    [0] => UMW Training Event
                )

我在这里做错了什么?

TA

1 个答案:

答案 0 :(得分:2)

当您回显对象时,它将转换为字符串。所以它会出现(因为SimpleXMLElement在内部定义了__toString()方法),就像你想要的那样 - 但$event变量的类型仍然是SimpleXMLElement类型的对象。

如果cast it为字符串,则可以轻松地将其作为字符串存储在数组中。尝试更改此行:

$ename = $event->EventName;

......对此:

$ename = (string) $event->EventName;