Link to home
Start Free TrialLog in
Avatar of benwiggy
benwiggyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Merge child arrays

I'm trying to find an elegant way of achieving the following:

- merge the "availability_" elements where the "flight_number" matches
- So that there is only one child array for each flight number (i.e. delete any further child arrays after the merge)
- Note: There won't be an occurrence where there are conflicting "availability_" values for the same flight number
- If no availability_ value is set for a given flight number, set it to 0.

I would like to turn this:

Array
(
    [0] => Array
        (
            [flight_number] => BA0179
            [availability_first] => 
            [availability_business] => 
            [availability_economy] => 99
            [flight_departure] => 2013-10-16 12:40
        )
    [1] => Array
        (
            [flight_number] => BA0213
            [availability_first] => 
            [availability_business] => 
            [availability_economy] => 6
            [flight_departure] => 2013-10-16 13:40
        )
    [2] => Array
        (
            [flight_number] => BA0179
            [availability_first] => 
            [availability_business] => 1
            [availability_economy] => 
            [flight_departure] => 2013-10-16 12:40
        )
)

Open in new window


into

Array
(
    [0] => Array
        (
            [flight_number] => BA0179
            [availability_first] => 0
            [availability_business] => 1
            [availability_economy] => 99
            [flight_departure] => 2013-10-16 12:40
        )
    [1] => Array
        (
            [flight_number] => BA0213
            [availability_first] => 0
            [availability_business] => 0
            [availability_economy] => 6
            [flight_departure] => 2013-10-16 13:40
        )
)

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I could see using usort() to sort by flight number, then using an iterator like foreach to walk the resulting array.  I'll try to show you a code example in a few minutes.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of benwiggy

ASKER

Thanks for the suggested solutions and feedback. I've realised that the easiest thing to do is use the flight number as the key (as suggested) - this enables me to include the availability counts as I build out the array.

As the suggested solutions helped me solve the problem, will award points.
Thanks for the points and thanks for using EE.  Going forward you might want to think about using object-oriented notation.  It is often more flexible than array notation.  You might also want to think about what happens if you have the same flight number on different days, or with different "legs" and the same flight number.  These factors may influence the data structure.

Best regards, ~Ray