1

I am trying to combine array(s) by key. So all sub arrays that start with the same sub-key will be combined into one sub array. Also, I want to keep the smallest value of the matched values.

Currently this is what I have:

$statement = Array
(
    [662_0] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 1290
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [662_1] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 295.00
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 995
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [662_2] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 1290
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [663_0] => Array
        (
            blah blah blah...
        )
);

This is what I would like to have:

$statement = Array
(
    [662] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 295.00
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 995
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [663] => Array
        (
            blah blah blah...
        )
);

Happy to provide more info if needed! Thanks!!!

Drewness
  • 5,004
  • 4
  • 32
  • 50
  • 1
    Why is this tagged [tag:mysql]? Did the data originate from a MySQL query? Perhaps the simplest solution is to alter it from that end? – eggyal Jun 11 '12 at 18:59
  • 2
    What have you tried thus far to sort and merge the arrays and return your result? – Jared Jun 11 '12 at 19:01
  • eggyal: Yes, the data was populated from a MySQL query. I have/am working on changing the method on that end as well.http://stackoverflow.com/questions/10983362/combine-results-for-sql-query Jrod: I have tried array_merge, array_multisort, and a combination with implode but cannot get the desired results. I am add whatever information is useful but there is a lot which is why I did not dump it all in to begin with. Thanks! – Drewness Jun 11 '12 at 19:26
  • I'm assuming that your keys are separated by underscore? What do you mean by smallest value? Smallest value of what? – Ja͢ck Jun 12 '12 at 05:47
  • They are separated by an underscore. Smallest value of the key. In the example I provided above. Array [662_x] key [12] has different values in each array; 1290, 995 and 1290. I would want key [12] in the new (merged) array to be 995 since it is the smallest of the values. – Drewness Jun 12 '12 at 14:08

1 Answers1

0

This code iterates over your original array, cuts by _ to get the combined identifier for the items and merges them together, and replaces the value in the resulting array if the new value is smaller than the one that is already set.

<?php
$original = array(
        '122_0' => array('k1'=>4, 'k2'=>2),
        '122_1' => array('k2'=>3, 'k3'=>3),
        '122_2' => array('k3'=>2, 'k4'=>4),
        '111_0' => array('k1'=>3, 'k2'=>3),
        '111_1' => array('k2'=>2, 'k3'=>2),
        '111_2' => array('k3'=>1, 'k4'=>1)
);

foreach ($original as $key=>$sub) {
    list($realkey, $num) = explode('_', $key);
    foreach ($sub as $subkey => $value) {
        if ( isset($result[$realkey][$subkey]) 
             && $value < $result[$realkey][$subkey] ) {
            $result[$realkey][$subkey] = $value;
        }
    }
}
print_r($result);
Kaii
  • 20,122
  • 3
  • 38
  • 60
  • This is awesome! I am SO close!!! But for some reason my `print_r($result);` is blank. Any thoughts..? – Drewness Jun 12 '12 at 16:12