0
$today = date("Y-m-d");
$firstday = date("Y-m"); // 20010310
$ip = List of IPs,i.e. 1.1.1.1, 1.1.1.2....;
foreach ($ip as $character){

$trafficinfo = json_decode(call_api($token, 'GET', "/traffic?ip=$character&type=month&from=$firstday-01&to=$today")); //Retrieve the infortmation we have for this server ID from the API (Power State, OS, etc)

output of $trafficinfo is following, also call_api is a function i have set with my credentials to the API (I don't think that's relevant).

Array
(
    [traffic] => stdClass Object
        (
            [type] => month
            [from] => 2020-06-01
            [to] => 2020-06-08
            [data] => stdClass Object
                (
                    [1.1.1.1] => stdClass Object
                        (
                            [in] => 0.36
                            [out] => 0.4296
                            [sum] => 0.7896
                        )

                )

        )

)
Array
(
    [traffic] => stdClass Object
        (
            [type] => month
            [from] => 2020-06-01
            [to] => 2020-06-08
            [data] => Array
                (
                )

        )

)
Array
(
    [traffic] => stdClass Object
        (
            [type] => month
            [from] => 2020-06-01
            [to] => 2020-06-08
            [data] => stdClass Object
                (
                    [1.1.1.2] => stdClass Object
                        (
                            [in] => 0.0003
                            [out] => 0.019
                            [sum] => 0.0193
                        )

                )

        )

)
Array
(
    [traffic] => stdClass Object
        (
            [type] => month
            [from] => 2020-06-01
            [to] => 2020-06-08
            [data] => Array
                (
                )

        )

)
Array
(
    [traffic] => stdClass Object
        (
            [type] => month
            [from] => 2020-06-01
            [to] => 2020-06-08
            [data] => Array
                (
                )

        )

)

Than i converted the json_decode to array and selected

$decodetraffic = (array)$trafficinfo;
$traffictraffic = (array)$decodetraffic['traffic']     
$trafficdata = (array)$traffictraffic['data'];
$trafficip = (array)$trafficdata["$character"];

$character is still taken from foreach, as this whole script is still under foreach. that gave me output to the following.

Array
(
    [in] => 0.36
    [out] => 0.4296
    [sum] => 0.7896
)
Array
(
)
Array
(
    [in] => 0.0003
    [out] => 0.019
    [sum] => 0.0193
)
Array
(
)
Array
(
)

Now all the array outputs are there of every IP, but all the array are separate and not giving me the required output.

Is there any way to sum all the [in]s and give out the answer?

I tried How to sum all column values in multi-dimensional array? however as all the output are different array, and not in one, i am unable to sum them.

Thank you.

Edit: Oh also i tried to array_merge($trafficip); but i get empty return of array.

EDIT: I was able to fix this, just had to add the whole array in a different array.

$today = date("Y-m-d");
$firstday = date("Y-m"); // 20010310
$newArray = [];

foreach ($ip as $character) {
$trafficinfos = json_decode(call_api($token, 'GET', "/traffic?ip=$character&type=month&from=$firstday-01&to=$today")); //Retrieve the infortmation we have for this server ID from the API (Power State, OS, etc)

$decodetraffic = (array) $trafficinfos;
$traffictraffic = (array) $decodetraffic['traffic'];
$trafficdata = (array) $traffictraffic['data'];
$trafficip = (array) $trafficdata["$character"];
$newArray[] = (array) $trafficip["in"];
$all = $newArray;
}
$sumArray = array();

foreach ($all as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}

print '<pre>';
print_r($sumArray);
print '</pre>';
Harsh
  • 152
  • 1
  • 1
  • 9
  • 1
    Duplicate of [How to sum all column values in multi-dimensional array?](https://stackoverflow.com/questions/1496682/how-to-sum-all-column-values-in-multi-dimensional-array) – MrUpsidown Jun 07 '20 at 21:55
  • @MrUpsidown, no it is not related, i have different array for each, not a same one. Can you open it back? – Harsh Jun 08 '20 at 07:33
  • 1) Start by posting valid code and 2) This has been answered multiple times already and 3) You haven't shown any attempt at doing what you ask for which is alone a reason for closing. – MrUpsidown Jun 08 '20 at 07:36
  • 1 - I did post the valid output 2 - i searched for it, even saw the link, but i cannot figure out a way out of it, 3 - i didn't get what you mean, here is what i tried - https://3v4l.org/eqq1v but it doesn't work – Harsh Jun 08 '20 at 07:41
  • `Array ( [in] => 0.2717 [out] => 0.3438 [sum] => 0.6155 ) Array ( ) Array ( [in] => 0.0003 [out] => 0.019 [sum] => 0.0193 ) Array ( ) Array ( )` is not valid PHP. Put all your arrays into another array and use the solution provided in the duplicate link. – MrUpsidown Jun 08 '20 at 07:43
  • @MrUpsidown i converted the json into array using `(array)$decodedoutput;` and then got this output. – Harsh Jun 08 '20 at 07:45
  • I also tried `array_merge` but as it is foreach and all the arrays are output of a single command, i am unable to merge it. – Harsh Jun 08 '20 at 07:47
  • Look, your question is too broad + unclear. You haven't provided any context or code which gave you this output + you haven't shown any attempt at doing what you asked for. Your question is closed. Think about it again, try to understand what your code does and how you could do what you want. If you still can't figure it out, open a new question with details, context, your attempts and why you think it fails. I will not vote to reopen this question (as it is). – MrUpsidown Jun 08 '20 at 07:49
  • @MrUpsidown, i have updated it and added everything i have on it, and why it is not working, as far as i think. I didn't add these details earlier because i was afraid it will be too long and maybe over informed post, sorry about that, can you check now and vote to reopen? – Harsh Jun 08 '20 at 08:02
  • The documentation on `array_merge()` mentions: *If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.* – MrUpsidown Jun 08 '20 at 08:13
  • The solution (again): put each array into another array (something like `$newArray = [$array1, $array2, $array3];`) then apply the solution provided in the duplicate link. – MrUpsidown Jun 08 '20 at 08:20
  • @MrUpsidown, i already thought about that too, but i am unable to do it, the $character are the IP out, so each output is generated by foreach, how can i put them in an array? I did `$trafficip = $trafficdata["$character"];` but the output is this - https://3v4l.org/OHK90 it still remains different array only, instead of combining into one. – Harsh Jun 08 '20 at 08:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215502/discussion-between-mrupsidown-and-harsh). – MrUpsidown Jun 08 '20 at 08:31

0 Answers0