0

I have below two arrays with strings and numbers,

I want to keep only one strings header and sum numeric value from each key value with another array.

I have tried many of the solutions available online but nothing found as required.

$array1 = 
Array
(
    [0] => Array
        (
            [0] => Out Of Warranty
            [1] => Total Orders
            [2] => Total Qty
            [3] => Canceled Orders
        )

    [1] => Array
        (
            [0] => Today<br/>(04-26-2020)
            [1] => 1
            [2] => 1
            [3] => 0
        )

    [2] => Array
        (
            [0] => Yesterday<br/>(04-25-2020)
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [3] => Array
        (
            [0] => This Week<br/>(04-20-2020 - 04-26-2020)
            [1] => 22
            [2] => 39
            [3] => 0
        )

    [4] => Array
        (
            [0] => Last Week<br/>(04-13-2020 - 04-19-2020)
            [1] => 7
            [2] => 7
            [3] => 0
        )

    [5] => Array
        (
            [0] => This Month<br/>(04-01-2020 - 04-26-2020)
            [1] => 29
            [2] => 46
            [3] => 0
        )

    [6] => Array
        (
            [0] => This Year<br/>(01-01-2020 - 04-26-2020)
            [1] => 30
            [2] => 47
            [3] => 0
        )

)

$array2 = 
Array
(
    [0] => Array
        (
            [0] => Out Of Warranty
            [1] => Total Orders
            [2] => Total Qty
            [3] => Canceled Orders
        )

    [1] => Array
        (
            [0] => Today<br/>(04-24-2020)
            [1] => 10
            [2] => 10
            [3] => 0
        )

    [2] => Array
        (
            [0] => Yesterday<br/>(04-23-2020)
            [1] => 7
            [2] => 7
            [3] => 0
        )

    [3] => Array
        (
            [0] => This Week<br/>(04-20-2020 - 04-24-2020)
            [1] => 51
            [2] => 51
            [3] => 0
        )

    [4] => Array
        (
            [0] => Last Week<br/>(04-13-2020 - 04-19-2020)
            [1] => 31
            [2] => 31
            [3] => 0
        )

    [5] => Array
        (
            [0] => This Month<br/>(04-01-2020 - 04-24-2020)
            [1] => 93
            [2] => 93
            [3] => 0
        )

    [6] => Array
        (
            [0] => This Year<br/>(01-01-2020 - 04-24-2020)
            [1] => 1281
            [2] => 1281
            [3] => 1
        )

)

Expected output as Strings should be use only once and numbers should be added to each other.

For example output should be 6 index i.e sum of 6 index from array1 and array2 -

[6] => Array
            (
                [0] => This Year<br/>(01-01-2020 - 04-26-2020)
                [1] => 1311
                [2] => 1328
                [3] => 1
            )

2 Answers 2

1

If your arrays are always sorted in the same order:

$newItems = [];
foreach ($array1 as $key => $item) {
    $newItems[] = [
        $item[0],
        $item[1] + $array2[$key][1],
        $item[2] + $array2[$key][2],
        $item[3] + $array2[$key][3],
    ];
}

If keys in arrays are in distinct orders:

$newItems = [];
foreach ($array1 as $item) {
    $name = $item[0];
    $newItems[$name] = $item;
}
foreach ($array2 as $item) {
    $name = $item[0];
    $newItems[$name][1] += $item[1];
    $newItems[$name][2] += $item[2];
    $newItems[$name][3] += $item[3];
}
// apply array_values to get 0-indexed array
$newItems = array_values($newItems);
Sign up to request clarification or add additional context in comments.

2 Comments

but while using 1st solution its giving output [0] => Array ( [0] => Out Of Warranty [1] => 0 [2] => 0 [3] => 0 ) which causing 0 index label empty
So, check if key is 0 and not add / skip values, you can do it, right?
0

Only iterate the elements that are necessary for your summing logic. Using 2 loops will be the most concise and deliberate way to sum columns [1], [2], and [3].

You can create a new output array, but merely adding the second array values to the first affords a simpler addition-assignment syntax.

Code: (Demo)

for ($i = 1, $size = count($array2); $i < $size; ++$i) {  // don't iterate [0] subarray (headers)
    for ($col = 1; $col <= 3; ++$col) {  // don't iterate [0] element (name)
        $array1[$i][$col] += $array2[$i][$col];
    }
}
var_export($array1);

Output:

array (
  0 => 
  array (
    0 => 'Out Of Warranty',
    1 => 'Total Orders',
    2 => 'Total Qty',
    3 => 'Canceled Orders',
  ),
  1 => 
  array (
    0 => 'Today<br/>(04-26-2020)',
    1 => 11,
    2 => 11,
    3 => 0,
  ),
  2 => 
  array (
    0 => 'Yesterday<br/>(04-25-2020)',
    1 => 7,
    2 => 7,
    3 => 0,
  ),
  3 => 
  array (
    0 => 'This Week<br/>(04-20-2020 - 04-26-2020)',
    1 => 73,
    2 => 90,
    3 => 0,
  ),
  4 => 
  array (
    0 => 'Last Week<br/>(04-13-2020 - 04-19-2020)',
    1 => 38,
    2 => 38,
    3 => 0,
  ),
  5 => 
  array (
    0 => 'This Month<br/>(04-01-2020 - 04-26-2020)',
    1 => 122,
    2 => 139,
    3 => 0,
  ),
  6 => 
  array (
    0 => 'This Year<br/>(01-01-2020 - 04-26-2020)',
    1 => 1311,
    2 => 1328,
    3 => 1,
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.