0

I have two arrays the structure is exactly the same only the values are different, like this..

// $array1
Array
(
    [Backlight] => Array
    (
        [Supply_Type] => Backlight
        [Value] => 100.00
        [Count] => 2
    )

    [Body Glass] => Array
    (
        [Supply_Type] => Body Glass
        [Value] => 100.00
        [Count] => 82
    )
)

// $array2
Array
(
    [Backlight] => Array
    (
        [Supply_Type] => Backlight
        [Value] => 200.00
        [Count] => 1
    )

    [Body Glass] => Array
    (
        [Supply_Type] => Body Glass
        [Value] => 150.00
        [Count] => 90
    )
)

The goal is to create one array from both with the highest value from "Value" and "Count" for each type ( Body Glass, Backlight ).

So it should look like this...

// $finalArray
Array
(
    [Backlight] => Array
    (
        [Supply_Type] => Backlight
        [Value] => 200.00
        [Count] => 2
    )

    [Body Glass] => Array
    (
        [Supply_Type] => Body Glass
        [Value] => 150.00
        [Count] => 90
    )
)

As there could be more than two arrays I was thinking I could do this with array_uintersect but as far as i'm aware, through trial and error, this only returns values from one array and compares against the rest, and feel that maybe i'm going down the wrong path.

I just thought I would add that this.

The problem started when using array_reduce, I needed to add up the values from two values ("value" and "count") in an array, ideally I wanted to just write...

        $sum = array_reduce($array, function ($a, $b) use($key)  {
            isset($a[$b[$key]]) ? $a[$b[$key]]["Value"] += $b["Value"]  : $a[$b[$key]]  = $b;
            isset($a[$b[$key]]) ? $a[$b[$key]]["Count"] += $b["Count"]  : $a[$b[$key]]  = $b;
            return $a;
        }); 

but the second assignment didnt add up correctly, it was adding the first array value twice, if I remember correctly.

So I settled for this which creates two or more arrays...

foreach($values as $value) {

        $sum[] = array_reduce($array, function ($a, $b) use($key, $value)  {
            isset($a[$b[$key]]) ? $a[$b[$key]][$value] += $b[$value]  : $a[$b[$key]]  = $b;
            return $a;
        }); 
    }

And also brings me to this current issue.

  • Are you using a database? If so do it there, if not then you should be using a database. – Lawrence Cherone Oct 02 '17 at 05:56
  • 1
    Just use a loop on arrays and catch the differents values before compare with the others and get the max the build your result array. – YaatSuka Oct 02 '17 at 05:59
  • Do you really mean to say that you have an Array full of Associative Arrays with `'Backlight'` and `'Body Glass'` properties... or you just have separate Arrays? – StackSlave Oct 02 '17 at 06:05
  • why dont you just arrange the array1, array2 ... into 4 arrays containing backlight-value, backlight-count, body class-value, body class-count and get the largest from all the 4 arrays and make one new array in the desired structure. – Viswanath Polaki Oct 02 '17 at 06:06
  • I have no access to a database to query this, yes the you are correct I had an Array full of Associative Arrays with 'Backlight' and 'Body Glass' properties until I used array_reduce to add them up and combine them. – Luke Dobner Oct 02 '17 at 06:12
  • @YaatSuka Thanks, I just thought there might of been something more elegant. – Luke Dobner Oct 02 '17 at 06:29

2 Answers2

1
 private function summary($array, $key, $values = array()) {

    foreach($values as $value) {
        $sum[] = array_reduce($array, function ($a, $b) use($key, $value)  {
            isset($a[$b[$key]]) ? $a[$b[$key]][$value] += $b[$value]  : $a[$b[$key]]  = $b;
            return $a;
        }); 
    }

    $finalSum = $sum[0];

    // sort into arrays...
    $sortArr = array();

    foreach($sum as $sumArr) {
        foreach($sumArr as $type => $typeArr) {
            foreach($typeArr as $k => $v) {
                if(in_array($k, $values)) {
                    $sortArr[$type][$k][] = $v; 
                }
            }
        }
    }

    // replace original array with highest values
    foreach($sortArr as $type => $fieldArr) {
        foreach($fieldArr as $fieldKey => $field) {
            $finalSum[$type][$fieldKey] = max($sortArr[$type][$fieldKey]);
        }
    }

    return $finalSum;
}
0

I have one simple suggestion for you -First get all key in single array using array_keys So you get array like this

$key_array = array(
    [0] => 'Backlight'
    [1] => 'Body Glass'
)
$final_array = [];
//than loop for key_array
foreach($key_array as $key=>$value){
    //check condition if array1 count bigger than push array1 otherwise push array2 object
    if($array1[$value]['Count']  > $array2[$value]['Count']){
        $temp_array = $array1[$value];
        if($array1[$value]['Value'] < $array2[$value]['Value'] ){
            $temp_array['Value'] = $array2[$value]['Value'];
        }
        array_push($final_array,$temp_array);

    }else{
        $temp_array = $array2[$value];
        if($array1[$value]['Value'] > $array2[$value]['Value'] ){
            $temp_array['Value'] = $array1[$value]['Value'];
        }
        array_push($final_array,$temp_array);
    }
}

Code for unknown number of array

$key_array = array_keys($array1);
$final_array = [];
$total_array = [$array1,$array2,......,$arrayN];
$temp_array = $total_array[0];
//than loop for key_array
foreach($key_array as $key=>$value){

    foreach ($total_array as $key=>$valueArr) {
        if($key != 0){
            // check condition if array1 count bigger than push array1 otherwise push array2 object
            if($valueArr[$value]['Count']  > $temp_array[$value]['Count']){
                $temp_array = $valueArr[$value];
                if($valueArr[$value]['Value'] < $temp_array[$value]['Value'] ){
                    $temp_array['Value'] = $temp_array[$value]['Value'];
                }
            }else{
                $temp_array = $temp_array[$value];
                if($valueArr[$value]['Value'] > $temp_array[$value]['Value'] ){
                    $temp_array['Value'] = $valueArr[$value]['Value'];
                }
            }
        }            
    }

    array_push($final_array,$temp_array);
}
bipin patel
  • 2,061
  • 1
  • 13
  • 22
  • Let me know if you need more help. – bipin patel Oct 02 '17 at 06:34
  • How would this work with an unknown amount of arrays, i was hoping there was a simpler way of doing it with a standard PHP function, but It doesn't look like there is one. – Luke Dobner Oct 02 '17 at 06:38
  • i have update my answer for unknown amout of array can you let me know it's working or not – bipin patel Oct 02 '17 at 07:34
  • I really appreciate you time and effort in helping me but I have just done it now. Thanks though, I have posted what I done just incase it helps anyone. – Luke Dobner Oct 02 '17 at 08:05