I am looking to extract the total sum of complete multidimensional array, including the embedded sub-arrays.
I understand you can extract the top level but it will not provide you the sub-arrays sum. Also each extraction of sub-array will only sum that specific sub-array.
The key names are intentionally left as numbers since my bigger script will constantly grow the array. Due to this growth of sub-array, the sub-arrays can be 1 but also 100.
Question:
How can I extract the value of the total multidimensional array including its sub-arrays.
[$array_1] works as expected:
<?php
/**
* [array_1]
* Array
*/
$array_1 = [1, 2, 4];
?>
<pre>
<?php
print_r($array_1);
echo("Sum complete array_1: " . array_sum($array_1));
[$array_2] Cannot get the total value.
/**
* [array_2]
* Multidimensional array.
*/
$array_2 = [
[
1, 2, 7
],
[
4, 5, 6
]
];
?>
<pre>
<?php
print_r($array_2);
// [$array_2].
echo("Sum complete array_2: " . array_sum($array_2)); // Returns zero.
echo("\n\n");
echo("Sum sub-array [0]:" . array_sum($array_2["0"])); // Returns 10.
echo("\n\n");
echo("\n\n");
Expected outcome [$array_2]:
25