I have an object with two arrays in JSON file :
"Data": {
"Server": ["a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c"],
"Count": ["12", "32", "7", "1", "67", "2", "3", "6", "5", "6", "5", "4"]
}
what I want to achive is to add all values of array 'count' for relative values in 'server' array and create new array with structure like this:
"Data": {
"Server": ["a", "b", "c"],
"Count": ["52", "78", "20"]
}
Can anyone help with this?
One way would be to do something like this:
<?php
$object = json_decode("your json here");
$server = $object->Server;
$count = $object->Count;
$tmp = [];
for( $i = 0; $i < $server; $i++){
$tmp[$server[$i]] += $count[$i];
}
$data = new stdClass();
$data->Server = array_keys($tmp);
$data->Count = array_values($tmp);
$json = json_encode($data);
But personally I would rather build a structure like:
{
"Data": {
"Servers": {
"a":52,
"b":78,
"c":20
}
}
}