PHP foreach没有携带密钥

时间:2013-12-29 12:02:06

标签: php session foreach

我正在尝试在stdObject类上创建一个foreach循环

$count = $_POST['count'];
json_decode($_SESSION['fl'][$count])

包含以下代码

stdClass Object
(
    [FlightSegment] => Array
        (
            [0] => stdClass Object
                (
                    [AirEquipType] => stdClass Object
                        (
                        )

                    [ArrivalAirportCode] => BOM
                    [ArrivalDateTime] => 2014-01-03T07:15:00
                    [DepartureAirportCode] => BLR
                    [DepartureDateTime] => 2014-01-03T05:45:00
                    [FlightNumber] => 322
                    [OperatingAirlineCode] => G8
                    [OperatingAirlineFlightNumber] => 322
                    [RPH] => stdClass Object
                        (
                        )

                    [StopQuantity] => 0
                    [airLineName] => GoAir
                    [airportTax] => 5136
                    [imageFileName] => http://live.arzoo.com/FlightWS/image/goair.gif
                    [viaFlight] => stdClass Object
                        (
                        )

                    [BookingClass] => stdClass Object
                        (
                            [Availability] => 1
                            [ResBookDesigCode] => R
                        )

                    [BookingClassFare] => stdClass Object
                        (
                            [adultFare] => 6100
                            [bookingclass] => R
                            [classType] => Economy
                            [farebasiscode] => x21RoTBkeV+cJeZY1rthLBbbjLwRoy6/oicf965tvXvjBVjANsWaFZWj53ainYSrB6LTu/WMZsBq
yV/P+wwX0/qcF2MJv3QC
                            [Rule] => This fare is Refundable |Booking Class : R|Cancellation Penalty:INR 1500/- per person per sector.|Date Change Penalty: In addition to the airlines date change penalty, we charge a service fee of Rs. 250 per passenger w.e.f 16th December 2013.|All fees are subject to change without notice.
                            [adultCommission] => 0
                            [childCommission] => 0
                            [commissionOnTCharge] => 0
                        )

                    [Discount] => 439
                    [airportTaxChild] => 0
                    [airportTaxInfant] => 0
                    [adultTaxBreakup] => 3650,225,1261
                    [childTaxBreakup] => 0,0,0
                    [infantTaxBreakup] => 0,0,0
                    [octax] => 10
                )

            [1] => stdClass Object
                (
                    [AirEquipType] => stdClass Object
                        (
                        )

                    [ArrivalAirportCode] => DEL
                    [ArrivalDateTime] => 2014-01-03T09:45:00
                    [DepartureAirportCode] => BOM
                    [DepartureDateTime] => 2014-01-03T07:35:00
                    [FlightNumber] => 327
                    [OperatingAirlineCode] => G8
                    [OperatingAirlineFlightNumber] => 327
                    [RPH] => stdClass Object
                        (
                        )

                    [StopQuantity] => 0
                    [airLineName] => GoAir
                    [airportTax] => 0
                    [imageFileName] => http://live.arzoo.com/FlightWS/image/goair.gif
                    [viaFlight] => stdClass Object
                        (
                        )

                    [BookingClass] => stdClass Object
                        (
                            [Availability] => 1
                            [ResBookDesigCode] => R
                        )

                    [BookingClassFare] => stdClass Object
                        (
                            [adultFare] => 1
                            [bookingclass] => R
                            [classType] => Economy
                            [farebasiscode] => x21RoTBkeV+cJeZY1rthLBbbjLwRoy6/oicf965tvXvjBVjANsWaFZWj53ainYSrRuiQx8hec5ML
Wf6yMF787w==
                            [Rule] => This fare is Refundable |Booking Class : R|Cancellation Penalty:INR 1500/- per person per sector.|Date Change Penalty: In addition to the airlines date change penalty, we charge a service fee of Rs. 250 per passenger w.e.f 16th December 2013.|All fees are subject to change without notice.
                            [adultCommission] => 0
                            [childCommission] => 0
                            [commissionOnTCharge] => 0
                        )

                    [Discount] => 0
                    [airportTaxChild] => 0
                    [airportTaxInfant] => 0
                    [adultTaxBreakup] => 0,0,0
                    [childTaxBreakup] => 0,0,0
                    [infantTaxBreakup] => 0,0,0
                    [octax] => 10
                )

        )

)

PHP

foreach(json_decode($_SESSION['fl'][$count]) as $key => $value){
    echo $key
}

我得到的输出FlightSegment任何人都可以猜到这里发生了什么?我正在尝试,因为中午之后什么都不会奏效。当我尝试手动访问数据时$value[0]->imageFileName它获得了第一个关键响应。

1 个答案:

答案 0 :(得分:1)

json_decode($_SESSION['fl'][$count])返回一个对象,而不是一个数组,但foreach只能用于迭代数组。如果你想循环FlightSegment,它应该是:

foreach(json_decode($_SESSION['fl'][$count])->FlightSegment as $key => $value) {
    ...
}