Jump to content

Combine similar arrays - additive


The Letter E

Recommended Posts

I have a brain twister in a script i'm working on. It have a multidimensional array that hold values to populate a google charts js script. To make it work as I plan I need to merge the data from similar arrays together.

 

I'll give an example and hopefully it will make more sense. Also, this page is being loaded over ajax so, the faster/smaller the better.

 

Example Array:

Array
(
    [0] => Array
        (
            [item] => 1
            [quant] => 2
            [price] => 12.5
            [day] => 18
            [onl] => 
        )

    [1] => Array
        (
            [item] => 1
            [quant] => 2
            [price] => 12.5
            [day] => 18
            [onl] => 
        )

    [2] => Array
        (
            [item] => 1
            [quant] => 5
            [price] => 12.5
            [day] => 17
            [onl] => 1
        )

    [3] => Array
        (
            [item] => 1
            [quant] => 12
            [price] => 12.5
            [day] => 19
            [onl] => 1
        )

    [4] => Array
        (
            [item] => 1
            [quant] => 4
            [price] => 12.5
            [day] => 21
            [onl] => 
        )

)

 

So since array 0 and 1 both have data from sales on the 18th for the same item I would want to merge them into a single array and leave all others in the parent array the same.

 

I apologize in advance if this doesn't make sense, I've been working too many hours. X|

 

If anyone knows of a good solution for this or perhaps a better approach, that would be great.

 

Thank  You,

 

E

Link to comment
Share on other sites

One approach is to make an associative array with the distinguishing feature as the key (aka a hash table).

 

$lookup_table = array();
foreach ($data as $d) {
  $key = $d['day'] . "-" . $d['item'];
  if ($lookup_table[$key]) {
    # Merge $d into existing data in $lookup_table[$key]
  } else {
    $lookup_table[$key] = $d;
  }
}

 

And then you can loop through $lookup_table to get the merged data.

Link to comment
Share on other sites

Thank you for the response. At this point I've decided that the best way to combine my data is before it even hits the script using the MySQL SUM(). It handles what the script was going to before it returns the initial rows, which is awesome.

 

Thanks for the reply. I will keep that method in mind. I'm sure it will come in handy in another situation.

 

E

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.