Jump to content

Combining 2 sets of data


gin

Recommended Posts

I have 2 sets of data I pulled from MySQL. Below is a small sample of the data.

$set1[0][item_id] = 'admin';
$set1[0][price] = 500;
$set1[1][item_id] = 'courier';
$set1[1][price] = 200;

$set2[0][item_id] = 'courier';
$set2[0][price] = 300;
$set2[1][item_id] = 'flight';
$set2[1][price] = 100;

 

I want to add the two sets of data together such that the prices of identical items are added together, like so:

$set3[0][item_id] = 'admin';
$set3[0][price] = 500;
$set3[1][item_id] = 'courier';
$set3[1][price] = 500;      // new price
$set3[2][item_id] = 'flight';
$set3[2][price] = 100;

 

Any kind of array merging or appending will not work due to the varying line numbers. Right now I'm resorting to comparing the two arrays with for loops, as below. Sadly my code only allows me to add the prices, but not insert new lines of data.

$set3 = $set1;
foreach ($set3 as $i => $idata) {
   foreach ($set2 as $jdata) {
      if ($idata['item_id'] == $jdata['item_id'])
         $set3[$i]['price'] += $jdata['price'];
   }
}

 

Please advise. Any help much appreciated!

Link to comment
Share on other sites

This might work:

$set = array()	
//get all the first set of data into the row: eg: $set['admin'] = '500';

//then get the second lot of data, on each fetch_row loop run:
$valueFoundFlagger = 1;
foreach($set as $key => $value)
	{
		if($key == $newIDFromMysql)
			{
				$set[$key] = $value + $newPriceFromMyDql
				$valueFoundFlagger = 2;
				break;		
			}
	}
if($valueFoundFlagger != 2)//then the new key from the second set of data was never in the first and a new entry into the $set array must be made
	{
		$set[$newIDFromMysql] = $newPriceFromMyDql
	}

Link to comment
Share on other sites

Thanks for the suggestions, freelance84, but rearranging the data in that manner doesn't quite work. While I'm only showing item_id & price, there are also 3 other fields, like so:

$set1[0][item_id] = 'admin';
$set1[0][price] = 500;
$set1[0][item_name] = 'Administrative charges';
$set1[0][section] = 'Administration';
$set1[0][sec_id] = 1;

 

I supposed I could pull out the id & price and sort it like you suggest, but then I'd just have to match it back to the rest of the data. Seems a bit circular?

 

As it is, there'll be more loops to get section subtotals and to format for output, though really, I'll probably try cram everything into one convoluted loop anyhow. One thing at a time :P

 

As for the original data arrays, they're being pulled from 3 different MySQL tables. As in data from 3 tables to get 1 set. And although I say I want to "combine 2 sets of data", in reality it's "2 or more". If it's possible to do any of this in the SQL code, it's beyond my can-barely-get-joins abilities.

Link to comment
Share on other sites

Why don't you use a JOIN in the query to return the data from each table as one set?

It's not 3 tables, each with a different set. I'm already JOINing the 3 to get just 1 set. Each set is a different row. Unless I'm misunderstanding you?

Link to comment
Share on other sites

Doing this in the query is by far your best bet.

 

The PHP to do it is ugly.

$set1[0]['item_id'] = 'admin';
$set1[0]['price'] = 500;
$set1[1]['item_id'] = 'courier';
$set1[1]['price'] = 200;

$set2[0]['item_id'] = 'courier';
$set2[0]['price'] = 300;
$set2[1]['item_id'] = 'flight';
$set2[1]['price'] = 100;

foreach( $set1 as &$a )
foreach( $set2 as $key => &$b )
	if( $a['item_id'] == $b['item_id']) {
		$a['price'] += $b['price'];
		unset( $set2[$key] );
	}
unset( $a, $b );

$merge = array_merge( $set1, $set2 );

print_r( $merge );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.