$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>';