dslk6326846 2012-01-28 15:05
浏览 9
已采纳

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

I'm having trouble accessing variable from an object. The object is coming from an API feed and a var_dump looks like this:

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

I want to be able to reorder the info that comes back into an array so that I can group the events by date and then order them by EventName. Below is my code:

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

If I echo out $event->EventName; it looks fine, but when I try to add it to an array it comes back like this:

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

What am I doing wrong here?

Ta

  • 写回答

1条回答 默认 最新

  • dscrb00804 2012-01-28 15:20
    关注

    When you echo an object, it is converted to a string. So it will appear (because SimpleXMLElement defines a __toString() method internally) like you want it to - but the type of the $event variable is still an object of type SimpleXMLElement.

    You can easily store it as a string in the array, if you cast it to a string. Try changing this line:

    $ename = $event->EventName;
    

    ...to this:

    $ename = (string) $event->EventName;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?