[HELP] How to make array like this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

[HELP] How to make array like this

Post by Grandong »

Hi...
i have array like :

Code: Select all

Array
(
    [arrivalAirport] => Array
        (
            [city] => DPS
            [cityCode] => 
            [code] => DPS
            [countryCode] => 
            [latitude] => -8.748169
            [longitude] => 115.167172
            [name] => Bali Ngurah Rai, Denpasar
            [timezone] => 
        )

    [departureAirport] => Array
        (
            [city] => CGK
            [cityCode] => 
            [code] => CGK
            [countryCode] => 
            [latitude] => -6.125567
            [longitude] => 106.655897
            [name] => Soekarno Hatta Intl, Jakarta
            [timezone] => 
        )

    [flightSegments] => Array
        (
            [0] => Array
                (
                    [aircraftCode] => 
                    [airline] => Array
                        (
                            [code] => QG
                            [name] => Citilink
                        )

                    [arrivalAirport] => Array
                        (
                            [city] => DPS
                            [cityCode] => 
                            [code] => DPS
                            [countryCode] => 
                            [latitude] => -8.748169
                            [longitude] => 115.167172
                            [name] => Bali Ngurah Rai, Denpasar
                            [timezone] => 
                        )

                    [departureAirport] => Array
                        (
                            [city] => CGK
                            [cityCode] => 
                            [code] => CGK
                            [countryCode] => 
                            [latitude] => -6.125567
                            [longitude] => 106.655897
                            [name] => Soekarno Hatta Intl, Jakarta
                            [timezone] => 
                        )

                    [duration] => 
                    [fares] => Array
                        (
                            [0] => Array
                                (
                                    [adminFee] => 0
                                    [airportTax] => 40000
                                    [availableCount] => 0
                                    [baggageAllowance] => 0
                                    [basicFare] => 935300
                                    [basicVat] => 93500
                                    [childFare] => 701500
                                    [childVat] => 70100
                                    [classOfService] => G
                                    [currency] => IDR
                                    [fareSellKey] => 0~G~~GG~PRFR~~1~X
                                    [fareType] => PR
                                    [fuelSurcharge] => 0
                                    [infantFare] => 200000
                                    [infantVat] => 20000
                                    [insurance] => 0
                                    [iwjr] => 5000
                                    [otherFee] => 0
                                    [serviceFee] => 0
                                )

                        )

                    [flightDesignator] => Array
                        (
                            [carrierCode] => QG
                            [flightNumber] => 9743
                        )

                    [includedBaggage] => 20
                    [includedMeal] => 0
                    [segmentSellKey] => QG~9743~ ~~CGK~12/11/2015 13:10~DPS~12/11/2015 15:55~
                    [selectedFare] => 
                    [sta] => 2015-12-11T15:55:00
                    [std] => 2015-12-11T13:10:00
                )

        )

    [journeySellKey] => QG~9743~ ~~CGK~12/11/2015 13:10~DPS~12/11/2015 15:55~
    [stopCount] => 0
)
how to make array at the top to like this:

Code: Select all

Array
(
    [arrivalAirport] => Array
        (
            [city] => DPS
            [cityCode] => 
            [code] => DPS
            [countryCode] => 
            [latitude] => -8.748169
            [longitude] => 115.167172
            [name] => Bali Ngurah Rai, Denpasar
            [timezone] => 
        )

    [departureAirport] => Array
        (
            [city] => CGK
            [cityCode] => 
            [code] => CGK
            [countryCode] => 
            [latitude] => -6.125567
            [longitude] => 106.655897
            [name] => Soekarno Hatta Intl, Jakarta
            [timezone] => 
        )

    [flightSegments] => Array
        (
            [0] => Array
                (
                    [aircraftCode] => 
                    [airline] => Array
                        (
                            [code] => QG
                            [name] => Citilink
                        )

                    [arrivalAirport] => Array
                        (
                            [city] => DPS
                            [cityCode] => 
                            [code] => DPS
                            [countryCode] => 
                            [latitude] => -8.748169
                            [longitude] => 115.167172
                            [name] => Bali Ngurah Rai, Denpasar
                            [timezone] => 
                        )

                    [departureAirport] => Array
                        (
                            [city] => CGK
                            [cityCode] => 
                            [code] => CGK
                            [countryCode] => 
                            [latitude] => -6.125567
                            [longitude] => 106.655897
                            [name] => Soekarno Hatta Intl, Jakarta
                            [timezone] => 
                        )

                    [duration] => 
                    [selectedFare] => Array
                        (
                                    [adminFee] => 0
                                    [airportTax] => 40000
                                    [availableCount] => 0
                                    [baggageAllowance] => 0
                                    [basicFare] => 935300
                                    [basicVat] => 93500
                                    [childFare] => 701500
                                    [childVat] => 70100
                                    [classOfService] => G
                                    [currency] => IDR
                                    [fareSellKey] => 0~G~~GG~PRFR~~1~X
                                    [fareType] => PR
                                    [fuelSurcharge] => 0
                                    [infantFare] => 200000
                                    [infantVat] => 20000
                                    [insurance] => 0
                                    [iwjr] => 5000
                                    [otherFee] => 0
                                    [serviceFee] => 0
                        )

                    [flightDesignator] => Array
                        (
                            [carrierCode] => QG
                            [flightNumber] => 9743
                        )

                    [includedBaggage] => 20
                    [includedMeal] => 0
                    [segmentSellKey] => QG~9743~ ~~CGK~12/11/2015 13:10~DPS~12/11/2015 15:55~
                    [fares] => 
                    [sta] => 2015-12-11T15:55:00
                    [std] => 2015-12-11T13:10:00
                )

        )

    [journeySellKey] => QG~9743~ ~~CGK~12/11/2015 13:10~DPS~12/11/2015 15:55~
    [stopCount] => 0
)
Condition:
- Change fares to selectedFare without [0]
thx...
Newbie The Passion for Learning
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: [HELP] How to make array like this

Post by Grandong »

Somebody Help me Please....
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: [HELP] How to make array like this

Post by Celauran »

What if there is more than one item in fares?
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: [HELP] How to make array like this

Post by Grandong »

Celauran wrote:What if there is more than one item in fares?
It will not happen, please give me example...
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: [HELP] How to make array like this

Post by Celauran »

So then just iterate over your flight segments, set selectedFare to fares[0], and unset fares
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: [HELP] How to make array like this

Post by Grandong »

Celauran wrote:So then just iterate over your flight segments, set selectedFare to fares[0], and unset fares
detail like this:
i will change :

Code: Select all

                    [fares] => Array
                        (
                            [0] => Array
                                (
                                    [adminFee] => 0
                                    [airportTax] => 40000
                                    [availableCount] => 0
                                    [baggageAllowance] => 0
                                    [basicFare] => 935300
                                    [basicVat] => 93500
                                    [childFare] => 701500
                                    [childVat] => 70100
                                    [classOfService] => G
                                    [currency] => IDR
                                    [fareSellKey] => 0~G~~GG~PRFR~~1~X
                                    [fareType] => PR
                                    [fuelSurcharge] => 0
                                    [infantFare] => 200000
                                    [infantVat] => 20000
                                    [insurance] => 0
                                    [iwjr] => 5000
                                    [otherFee] => 0
                                    [serviceFee] => 0
                                )

                        )
to

Code: Select all

[fares] =>
then set selectedFare

Code: Select all

                    [selectedFare] => Array
                        (
                                    [adminFee] => 0
                                    [airportTax] => 40000
                                    [availableCount] => 0
                                    [baggageAllowance] => 0
                                    [basicFare] => 935300
                                    [basicVat] => 93500
                                    [childFare] => 701500
                                    [childVat] => 70100
                                    [classOfService] => G
                                    [currency] => IDR
                                    [fareSellKey] => 0~G~~GG~PRFR~~1~X
                                    [fareType] => PR
                                    [fuelSurcharge] => 0
                                    [infantFare] => 200000
                                    [infantVat] => 20000
                                    [insurance] => 0
                                    [iwjr] => 5000
                                    [otherFee] => 0
                                    [serviceFee] => 0
                        )
Please, Give me example code....
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: [HELP] How to make array like this

Post by Celauran »

I just explained how to do it. What have you tried and what isn't working?
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: [HELP] How to make array like this

Post by Grandong »

Celauran wrote:I just explained how to do it. What have you tried and what isn't working?
i try using array_map
but not working
Newbie The Passion for Learning
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: [HELP] How to make array like this

Post by Christopher »

Something like?

Code: Select all

$selectedFares = array();
foreach ($fares as $fare) {
    $selectedFares[] = $fare;
}
Or as simple as:

Code: Select all

$selectedFare = $fares[0];
(#10850)
Post Reply