无法从客户端解析回答案

时间:2018-12-03 21:28:27

标签: php arrays xml parsing

美好的一天,

我正在尝试从客户那里读回我的答案,我在下面以数组格式发布了该客户。我开始阅读并循环输入答案,但有时我迷失了方向。

我尝试了不同的方法,但没有成功,并寻求帮助。如何正确地应用foreach,才能正确阅读以下答案?

答案以xml格式从客户端返回,但与print_r发布以更好地理解。我需要一些帮助指出我在做什么错吗?我希望打印所有返回的框,例如(大尺寸,重量及其内容

answer:

SimpleXMLElement Object ( [ratesAndServicesResponse] => SimpleXMLElement Object ( [statusCode] => 1 [statusMessage] => OK [requestID] => 61639309 [handling] => 3.0 [language] => 0 [product] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1020 [sequence] => 1 ) [name] => Expedited [rate] => 34.24 [shippingDate] => 2018-12-05 [deliveryDate] => 2018-12-06 [deliveryDayOfWeek] => 5 [nextDayAM] => false [packingID] => P_0 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1010 [sequence] => 2 ) [name] => Regular [rate] => 34.24 [shippingDate] => 2018-12-05 [deliveryDate] => 2018-12-07 [deliveryDayOfWeek] => 6 [nextDayAM] => false [packingID] => P_0 ) ) [packing] => SimpleXMLElement Object ( [packingID] => P_0 [box] => Array ( [0] => SimpleXMLElement Object ( [name] => Extra Large [weight] => 7.8 [expediterWeight] => 7.8 [length] => 40.0 [width] => 30.5 [height] => 21.6 [packedItem] => Array ( [0] => SimpleXMLElement Object ( [quantity] => 4 [description] => Speakers ) [1] => SimpleXMLElement Object ( [quantity] => 1 [description] => CoffeMaker ) ) ) [1] => SimpleXMLElement Object ( [name] => Extra Large [weight] => 7.15 [expediterWeight] => 7.15 [length] => 40.0 [width] => 30.5 [height] => 21.6 [packedItem] => Array ( [0] => SimpleXMLElement Object ( [quantity] => 3 [description] => PowoGlove ) [1] => SimpleXMLElement Object ( [quantity] => 1 [description] => Nomopoly ) [2] => SimpleXMLElement Object ( [quantity] => 4 [description] => Iphune ) ) ) [2] => SimpleXMLElement Object ( [name] => Large [weight] => 3.95 [expediterWeight] => 3.95 [length] => 38.1 [width] => 30.5 [height] => 9.5 [packedItem] => Array ( [0] => SimpleXMLElement Object ( [quantity] => 3 [description] => Iphune ) [1] => SimpleXMLElement Object ( [quantity] => 6 [description] => RobbikCube ) ) ) ) ) [shippingOptions] => SimpleXMLElement Object ( [insurance] => No [deliveryConfirmation] => Yes [signature] => No ) [comment] => SimpleXMLElement Object ( ) [nearestPostalOutlet] => Array ( [0] => SimpleXMLElement Object ( [postalOutletSequenceNo] => 1 [distance] => 1.1 [outletName] => C/O: OTTAWA B PO [businessName] => OTTAWA B PO [postalAddress] => SimpleXMLElement Object ( [addressLine] => 59 SPARKS ST [postalCode] => K1P5A0 [municipality] => OTTAWA [province] => ON ) [phoneNumber] => 613-844-1545 [businessHours] => Array ( [0] => SimpleXMLElement Object ( [dayId] => 1 [dayOfWeek] => SUN [time] => closed ) [1] => SimpleXMLElement Object ( [dayId] => 2 [dayOfWeek] => MON [time] => 08:00-18:00 ) [2] => SimpleXMLElement Object ( [dayId] => 3 [dayOfWeek] => TUE [time] => 08:00-18:00 ) [3] => SimpleXMLElement Object ( [dayId] => 4 [dayOfWeek] => WED [time] => 08:00-18:00 ) [4] => SimpleXMLElement Object ( [dayId] => 5 [dayOfWeek] => THU [time] => 08:00-18:00 ) [5] => SimpleXMLElement Object ( [dayId] => 6 [dayOfWeek] => FRI [time] => 08:00-18:00 ) [6] => SimpleXMLElement Object ( [dayId] => 7 [dayOfWeek] => SAT [time] => closed ) ) ) [1] => SimpleXMLElement Object ( [postalOutletSequenceNo] => 2 [distance] => 1.3 [outletName] => C/O: PLACE DU CENTRE [businessName] => PAPETERIE THIBO [postalAddress] => SimpleXMLElement Object ( [addressLine] => 200 PROMENADE DU PORTAGE [postalCode] => J8X4H0 [municipality] => HULL [province] => QC ) [phoneNumber] => 819-777-8284 [businessHours] => Array ( [0] => SimpleXMLElement Object ( [dayId] => 1 [dayOfWeek] => SUN [time] => closed ) [1] => SimpleXMLElement Object ( [dayId] => 2 [dayOfWeek] => MON [time] => 08:00-17:30 ) [2] => SimpleXMLElement Object ( [dayId] => 3 [dayOfWeek] => TUE [time] => 08:00-17:30 ) [3] => SimpleXMLElement Object ( [dayId] => 4 [dayOfWeek] => WED [time] => 08:00-17:30 ) [4] => SimpleXMLElement Object ( [dayId] => 5 [dayOfWeek] => THU [time] => 08:00-17:30 ) [5] => SimpleXMLElement Object ( [dayId] => 6 [dayOfWeek] => FRI [time] => 08:00-17:30 ) [6] => SimpleXMLElement Object ( [dayId] => 7 [dayOfWeek] => SAT [time] => 10:00-12:00 ) ) ) ) ) )

在“ PackingID”级别不知所措时,我不知道如何循环显示所有带有项目数量和说明的包装箱

我的php文件的一部分,我开始捕获并显示接收到的数据。

if (!$xml) {
            echo "Failed loading XML\n";
            echo "$xml\n";
            foreach (libxml_get_errors() as $error) {
                echo "\t" . $error->message;
            }
        } else {
        if ($xml->{'ratesAndServicesResponse'} ) {
            $ratesAndServicesResponse = $xml->{'ratesAndServicesResponse'};
 //      echo "<center><font color='white'><h3>Status Code " . $ratesAndServicesResponse->{"statusCode"} . "</font></h3></center>";
 //      echo "<center><font color='white'><h3>Status Message " . $ratesAndServicesResponse->{"statusMessage"} . "</font></h3></center>";
 //       echo "<center><font color='white'><h3>request ID " . $ratesAndServicesResponse->{"requestID"} . "</font></h3></center>";
 //       echo "<center><font color='white'><h3>handling " . $ratesAndServicesResponse->{"handling"} . "</font></h3></center>";
 //       echo "<center><font color='white'><h3>language " . $ratesAndServicesResponse->{"language"} . "</font></h3></center>";



             foreach ($ratesAndServicesResponse->{'product'} as $Product) {
            echo "<center><font size='2' color='white'><h3>Product:" . $Product->{'name'}. "</font></h3></center>";
            echo "<center><font size='2' color='white'><h3>Price:" . $Product->{'rate'} . "</font></h3></center>";
            echo "<center><font size='2' color='white'><h3>Shipping Date: " . $Product->{'shippingDate'} . "</font></h3></center>";
            echo "<center><font size='2' color='white'><h3>Delivery Date: " . $Product->{'deliveryDate'} . "</font></h3></center>";
      //      echo "<center><font <font size='2' color='white'><h3>delivery Day Of Week: " . $Product->{'deliveryDayOfWeek'} . "</font></h3></center>";
      //       echo "<center><font <font size='2' color='white'><h3>next Day AM: " . $Product->{'nextDayAM'} . "</font></h3></center>";
             echo "<center><font color='white'><h3>packingID: " . $Product->{'packingID'} . "</font></h3></center>";
              }


             foreach ($ratesAndServicesResponse->{'packing'} as $packing){
             echo "<center><font color='white'><h3>packingID: " . $packing->{'packingID'} . "</font></h3></center>";


             }


             foreach ($ratesAndServicesResponse->{'packing'} as $PackingID) {                            
             echo "<center><font size='2' color='white'><h3>Name: " . $PackingID->{'box'}->{'name'} . "</font></h3></center>";                  
             echo "<center><font size='2' color='white'><h3>Weight:" . $PackingID->{'box'}->{'weight'} . "</font></h3></center>";     
             echo "<center><font size='2' color='white'><h3>Length: " . $PackingID->{'box'}->{'length'} . "</font></h3></center>";
             echo "<center><font size='2' color='white'><h3>Width: " . $PackingID->{'box'}->{'width'} . "</font></h3></center>";
             echo "<center><font size='2' color='white'><h3>Height: " . $PackingID->{'box'}->{'height'} . "</font></h3></center>";  
             }

             //im here
             foreach ($ratesAndServicesResponse->{'packing'}->{'box'}->{'packedItem'} as $packeditem){
             echo "<center><font size='2' color='white'><h3>quantity: " . $packeditem->{'quantity'} . "</font></h3></center>";
             echo "<center><font size='2' color='white'><h3>Description: " . $packeditem->{'description'} . "</font></h3></center>";

            }






            // ($ratesAndServicesResponse->{'shippingOptions'} as $shippingOptions);
    //         echo "<center><font color='white'><h3>insurance: " . $shippingOptions->{'insurance'} . "</font></h3></center>";
    //         echo "<center><font color='white'><h3>deliveryConfirmation: " . $shippingOptions->{'deliveryConfirmation'} . "</font></h3></center>";
    //         echo "<center><font color='white'><h3>signature: " . $shippingOptions->{'signature'} . "</font></h3></center>";



            // ($ratesAndServicesResponse->{'nearestPostalOutlet'} as $nearestPostalOutlet);
   //          echo "<center><font color='white'><h3>postalOutletSequenceNo: " . $nearestPostalOutlet->{'postalOutletSequenceNo'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>distance: " . $nearestPostalOutlet->{'distance'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>outletName: " . $nearestPostalOutlet->{'outletName'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>businessName: " . $nearestPostalOutlet->{'businessName'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>addressLine: " . $nearestPostalOutlet->{'postalAddress'}->{'addressLine'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>postalCode: " . $nearestPostalOutlet->{'postalAddress'}->{'postalCode'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>municipality: " . $nearestPostalOutlet->{'postalAddress'}->{'municipality'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>province: " . $nearestPostalOutlet->{'postalAddress'}->{'province'} . "</font></h3></center>";
   //          echo "<center><font color='white'><h3>phoneNumber: " . $nearestPostalOutlet->{'phoneNumber'} . "</font></h3></center>";            

             // ($ratesAndServicesResponse->{'nearestPostalOutlet'}->{'businessHours'} as $businessHours)
         //    echo "<center><font color='white'><h3>dayId: " . $businessHours->{'dayId'} . "</font></h3></center>";
         //    echo "<center><font color='white'><h3>dayOfWeek: " . $businessHours->{'dayOfWeek'} . "</font></h3></center>";
          //   echo "<center><font color='white'><h3>time: " . $businessHours->{'time'} . "</font></h3></center>";


        }
            else {
                $messages = 'Why?';
                foreach ($messages as $message) {
                    echo "<font color ='white'>Error Code:" . $message->code . "</font>\n";
                    echo "<font color ='white'>Error Msg:" . $message->description . "</font>\n\n";
                }
            }

    }

我还以数组的形式构建了整个答案,以更好地理解

    1
    OK
    61639309
    3.0
    0

        Expedited
        34.24
        2018-12-05
        2018-12-06
        5
        false
        P_0


        Regular
        34.24
        2018-12-05
        2018-12-07
        6
        false
        P_0




        P_0

            Extra Large
            7.8
            7.8
            40.0
            30.5
            21.6

                4
                Speakers


                1
                CoffeMaker






            Extra Large
            7.15
            7.15
            40.0
            30.5
            21.6

                3
                PowoGlove


                1
                Nomopoly


                4
                Iphune






            Large
            3.95
            3.95
            38.1
            30.5
            9.5

                3
                Iphune


                6
                RobbikCube









        No
        Yes
        No






1
1.1
C/O: OTTAWA  B PO
OTTAWA  B PO

    59 SPARKS ST    
    K1P5A0
    OTTAWA
    ON

613-844-1545

    1
    SUN
    closed


    2
    MON
    08:00-18:00


    3
    TUE
    08:00-18:00


    4
    WED
    08:00-18:00


    5
    THU
    08:00-18:00


    6
    FRI
    08:00-18:00


    7
    SAT
    closed




2
1.3
C/O: PLACE DU CENTRE
PAPETERIE THIBO

    200 PROMENADE DU PORTAGE    
    J8X4H0
    HULL
    QC

819-777-8284

    1
    SUN
    closed


    2
    MON
    08:00-17:30


    3
    TUE
    08:00-17:30


    4
    WED
    08:00-17:30


    5
    THU
    08:00-17:30


    6
    FRI
    08:00-17:30


    7
    SAT
    10:00-12:00

我可以打印P0的第一个实例,但不能打印其余的,为什么?如何修复foreach或正确申请?

  P_0

        Extra Large        <---I can cpture and display
        7.8
        7.8
        40.0
        30.5
        21.6

            4
            Speakers


            1
            CoffeMaker






        Extra Large  <--I am unable to capture the rest
        7.15
        7.15
        40.0
        30.5
        21.6

            3
            PowoGlove


            1
            Nomopoly


            4
            Iphune






        Large  <--I am unable to capture the rest
        3.95
        3.95
        38.1
        30.5
        9.5

            3
            Iphune


            6
            RobbikCube

XML响应

<eparcel>
<ratesAndServicesResponse>
    <statusCode>1</statusCode>
    <statusMessage>OK</statusMessage>
    <requestID>61641078</requestID>
    <handling>1.0</handling>
    <language>0</language>
    <product id="1020" sequence="1">
        <name>Expedited</name>
        <rate>24.96</rate>
        <shippingDate>2018-12-05</shippingDate>
        <deliveryDate>2018-12-06</deliveryDate>
        <deliveryDayOfWeek>5</deliveryDayOfWeek>
        <nextDayAM>false</nextDayAM>
        <packingID>P_0</packingID>
    </product>
    <product id="1010" sequence="2">
        <name>Regular</name>
        <rate>24.5</rate>
        <shippingDate>2018-12-05</shippingDate>
        <deliveryDate>2018-12-10</deliveryDate>
        <deliveryDayOfWeek>2</deliveryDayOfWeek>
        <nextDayAM>false</nextDayAM>
        <packingID>P_0</packingID>
    </product>
    <packing>
        <packingID>P_0</packingID>
        <box>
            <name>SUPER BOX</name>
            <weight>1.741</weight>
            <expediterWeight>1.741</expediterWeight>
            <length>22.0</length>
            <width>17.0</width>
            <height>13.0</height>
            <packedItem>
                <quantity>1</quantity>
                <description>KAO Diskettes</description>
            </packedItem>
        </box>
        <box>
            <name>My Ready To Ship Item</name>
            <weight>2.0</weight>
            <expediterWeight>1.5</expediterWeight>
            <length>30.0</length>
            <width>20.0</width>
            <height>20.0</height>
            <packedItem>
                <quantity>1</quantity>
                <description>My Ready To Ship Item</description>
            </packedItem>
        </box>
    </packing>
    <shippingOptions>
        <insurance>No</insurance>
        <deliveryConfirmation>Yes</deliveryConfirmation>
        <signature>No</signature>
    </shippingOptions>
    <comment/>
    <nearestPostalOutlet>
        <postalOutletSequenceNo>1</postalOutletSequenceNo>
        <distance>0.7</distance>
        <outletName>C/O: CP ATWATER</outletName>
        <businessName>BRUNET ATWATER</businessName>
        <postalAddress>
            <addressLine>151 ATWATER</addressLine>
            <postalCode>H3J2J0</postalCode>
            <municipality>MONTR?AL</municipality>
            <province>QC</province>
        </postalAddress>
        <phoneNumber>514-935-5637</phoneNumber>
        <businessHours>
            <dayId>1</dayId>
            <dayOfWeek>SUN</dayOfWeek>
            <time>closed</time>
        </businessHours>
        <businessHours>
            <dayId>2</dayId>
            <dayOfWeek>MON</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>3</dayId>
            <dayOfWeek>TUE</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>4</dayId>
            <dayOfWeek>WED</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>5</dayId>
            <dayOfWeek>THU</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>6</dayId>
            <dayOfWeek>FRI</dayOfWeek>
            <time>09:00-19:00</time>
        </businessHours>
        <businessHours>
            <dayId>7</dayId>
            <dayOfWeek>SAT</dayOfWeek>
            <time>10:00-15:00</time>
        </businessHours>
    </nearestPostalOutlet>
    <nearestPostalOutlet>
        <postalOutletSequenceNo>2</postalOutletSequenceNo>
        <distance>0.9</distance>
        <outletName>C/O: BP MONTREAL D</outletName>
        <businessName>BP MONTREAL D</businessName>
        <postalAddress>
            <addressLine>1695 GRAND TRUNK</addressLine>
            <postalCode>H3K1J0</postalCode>
            <municipality>MONTREAL</municipality>
            <province>QC</province>
        </postalAddress>
        <phoneNumber>514-846-5391</phoneNumber>
        <businessHours>
            <dayId>1</dayId>
            <dayOfWeek>SUN</dayOfWeek>
            <time>closed</time>
        </businessHours>
        <businessHours>
            <dayId>2</dayId>
            <dayOfWeek>MON</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>3</dayId>
            <dayOfWeek>TUE</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>4</dayId>
            <dayOfWeek>WED</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>5</dayId>
            <dayOfWeek>THU</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>6</dayId>
            <dayOfWeek>FRI</dayOfWeek>
            <time>09:30-17:30</time>
        </businessHours>
        <businessHours>
            <dayId>7</dayId>
            <dayOfWeek>SAT</dayOfWeek>
            <time>closed</time>
        </businessHours>
    </nearestPostalOutlet>
</ratesAndServicesResponse>
</eparcel>

2 个答案:

答案 0 :(得分:2)

您需要在box元素上循环,然后在每个框中的packedItem上循环。像这样:

    foreach ($ratesAndServicesResponse->packing->box as $box) {
        echo "box name: " . $box->name . "\n";
        foreach ($box->packedItem as $packeditem) {
            echo "<center><font size='2' color='white'><h3>quantity: " . $packeditem->quantity . "</font></h3></center>\n";
            echo "<center><font size='2' color='white'><h3>Description: " . $packeditem->description . "</font></h3></center>\n";
        }
    }
}

输出:

box name: SUPER BOX 
<center><font size='2' color='white'><h3>quantity: 1</font></h3></center> 
<center><font size='2' color='white'><h3>Description: KAO Diskettes</font></h3></center>
box name: My Ready To Ship Item 
<center><font size='2' color='white'><h3>quantity: 1</font></h3></center> 
<center><font size='2' color='white'><h3>Description: My Ready To Ship Item</font></h3></center>

Demo on 3v4l.org

请注意,您不需要使用->{'element'}表示法,除非元素名称中包含特殊字符,否则就可以使用->element

答案 1 :(得分:2)

我相信XPath可以为您提供“所有盒子中的所有物品”的扁平阵列,请尝试:

foreach ($ratesAndServicesResponse->xpath("packing/box/packedItem") as $packeditem) {
  echo "<center><font size='2' color='white'><h3>quantity: " . $packeditem->quantity . "</font></h3></center>\n";
  echo "<center><font size='2' color='white'><h3>Description: " . $packeditem->description . "</font></h3></center>\n";
}