Jump to content

Sorting multi level Associative arrays


AMERLOC

Recommended Posts

Hi again,

 

Guys I am New to PHP and I am having a real problem sorting  associative arrays when they have arrays of arrays.

 

the array is like this

 

Array
(
    [AirAvailabilityResults] => Array
        (
            [_size] => 9
            [AirAvailabilityReply] => Array
                (
                    [0] => Array
                        (
                            [supplierType] => S
                            [RateInfo] => Array
                                (
                                    [nativeBaseFare] => 763.0
                                    [nativeTotalPrice] => 1753.08
                                    [nativeCurrencyCode] => USD
                                    [displayBaseFare] => 763.0
                                    [displayTotalPrice] => 1753.08
                                    [displayCurrencyCode] => USD
                                )

                            [FlightSegment] => Array
                                (
                                    [0] => Array
                                        (
                                            [segmentOutgoing] => true
                                            [airlineCode] => US
                                            [airline] => US AIRWAYS
                                            [flightNumber] => 1498
                                                                                    )

                                )

                            [tripType] => R
                            [ticketType] => E
                        )

                    [1] => Array
                        (
                            [supplierType] => S
                            [RateInfo] => Array
                                (
                                    [nativeBaseFare] => 763.0
                                    [nativeTotalPrice] => 1767.08
                                    [nativeCurrencyCode] => USD
                                    [displayBaseFare] => 763.0
                                    [displayTotalPrice] => 1767.08
                                    [displayCurrencyCode] => USD
                                )

                            [FlightSegment] => Array
                                (
                                    [0] => Array
                                        (
                                            [segmentOutgoing] => true
                                            [airlineCode] => US
                                            [airline] => US AIRWAYS
                                            [flightNumber] => 1498
                                       
                                                                   )

                                )

                            [tripType] => R
                            [ticketType] => E
                        )
                                    
            [cacheKey] => 20a9f112:1239917b36e:-b84
            [cacheLocation] => 10.186.168.43:7303
        )

)

 

I am trying to sort the array [AirAvailabilityReply] by the [RateInfo] array.

 

in this example it only shows how to sort an array not an array by a child array:

function (compare ($x, $y){
     if ($x[1] == $y[1]) {
         return 0;
     } else if ($x[1] < $y[1]) {
         return -1;
     } else {
        return 1;
     }
}

usort ($products, 'compare');

 

 

I have read the forum but there is something I am missing. for example there are 5 availabilty arrays containing each 5 other arrays where I want to sort them via the RateInfo Array so When I do my loops they will be printed in the order cheapest fare.

 

I have finished the program now but and missing the this freakking sorting.

 

Please give me a clue on how to do this and an example would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/173524-sorting-multi-level-associative-arrays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.