Solved

Merge child arrays

Posted on 2012-12-28
5
207 Views
Last Modified: 2012-12-28
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

0
Comment
Question by:benwiggy
  • 3
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
This looks OK to me.  See http://www.laprbass.com/RAY_temp_benwiggy.php

<?php // RAY_temp_benwiggy.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA
$td = 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'
        )
)
;
// DOES IT LOOK RIGHT? (YES)
print_r($td);

// SORT BY FLIGHT NUMBER
function flight_sort($a, $b)
{
    if ($a["flight_number"] == $b["flight_number"]) return 0;
    return ($a["flight_number"] < $b["flight_number"]) ? -1 : 1;
}
usort($td, 'flight_sort');

// CREATE A TEMPLATE WITH ZERO VALUES
$tmp = $td[0];
foreach ($tmp as $key => $val) $tmp[$key] = 0;

// SET UP FOR THE ITERATOR
$sum = $tmp;
$out = array();
$old_flight_number = FALSE;

// ITERATE OVER THE TEST DATA
foreach ($td as $now)
{
    // IF A NEW FLIGHT NUMBER
    if ($now['flight_number'] != $old_flight_number)
    {
        // SAVE THE LAST SUMMARY
        $out[] = $sum;

        // SET A NEW FLIGHT NUMBER
        $old_flight_number = $now['flight_number'];

        // COPY THE TEMPLATE
        $sum = $tmp;
    }

    // WHETHER OR NOT A NEW FLIGHT NUMBER, FILL IN THE PARTS
    foreach ($now as $key => $val)
    {
        if (substr($key,0,1) == 'f') $sum[$key] = $val;
        if (substr($key,0,1) == 'a') $sum[$key] += $val;
    }
}

// APPEND LAST FLIGHT
$out[] = $sum;

// DECLOP THE TEMPLATE
unset($out[0]);

// SHOW THE WORK PRODUCT
print_r($out);

Open in new window

Best regards, ~Ray
0
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
Comment Utility
My suggestion would be to convert the data into:
$flights=Array
(
    [BA0179] => Array
        (
            [availability_first] => 0
            [availability_business] => 1
            [availability_economy] => 99
            [flight_departure] => 2013-10-16 12:40
        )
    [BA0213] => Array
        (
            [availability_first] => 0
            [availability_business] => 0
            [availability_economy] => 6
            [flight_departure] => 2013-10-16 13:40
        )
)

Open in new window


The advantages of the new structure are:
a. fewer elements - the [flight_number] key no longer exists.  Intead, the actual flight number becomes the top-level key.
b. easier to find/search details for a flight number - ex:
$id='BA0213';
if( isset($flights[$id]) )
{
   //if it makes it here, the flight exists
   print_r($flights[$id]);
}

Try the code below:
$data=Array(
    '0' => Array(
            'flight_number' => 'BA0179'
            ,'availability_first' => NULL
            ,'availability_business' => NULL
            ,'availability_economy' => 99
            ,'flight_departure' => '2013-10-16 12:40'
        )
    ,'1' => Array(
            'flight_number' => 'BA0213'
            ,'availability_first' => NULL
            ,'availability_business' => NULL
            ,'availability_economy' => 6
            ,'flight_departure' => '2013-10-16 13:40'
        )
    ,'2' => Array(
            'flight_number' => 'BA0179'
            ,'availability_first' => NULL
            ,'availability_business' => 1
            ,'availability_economy' => NULL
            ,'flight_departure' => '2013-10-16 12:40'
        )
);

$flights=Array();
foreach($data as $f)
{
	$id=$f['flight_number'];

        //this gets rid of the flight_number key/element
	unset($f['flight_number']);

	$f['availability_first']=(int)$f['availability_first'];
	$f['availability_business']=(int)$f['availability_business'];
	$f['availability_economy']=(int)$f['availability_economy'];

	if(!isset($flights[$id]))
	{
		$flights[$id]=Array();
	}
	else
	{
		$f['availability_first']=max($f['availability_first'],$flights[$id]['availability_first']);
		$f['availability_business']=max($f['availability_business'],$flights[$id]['availability_business']);
		$f['availability_economy']=max($f['availability_economy'],$flights[$id]['availability_economy']);
	}
	$flights[$id]=array_merge($flights[$id], $f);
	
}
unset($data);
echo '<pre>';
print_r($flights);

Open in new window

0
 
LVL 6

Author Comment

by:benwiggy
Comment Utility
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now