I have to create an array in PHP with information that comes from a json.
Currently I have my code that creates an array where it keeps the date "10-15-2018" with its total number of messages.
Now I need to modify my array to be able to capture the day with its total messages, sent messages and received messages.
Within the same array that saves the hours with your total messages, sent messages and received messages.
The problem is that I can not do it because I do not know how to start doing it. someone can guide me how to do it.
If you find the day or time in the array that increases the number by one in total messages, sent messages and received messages as found.
In the json comes "type": which is "IN" which are input messages and "OUT" output messages and creation_date
which is the date when a message is sent or received.
This is my current code:
public function Informe_Completo(Request $request)
{
$empresa = self::Empresa_Citrup();
$resul = self::Api_Extrae_Mensajes($empresa);
$in = 0;
$out = 0;
$total = 0;
$fecha = array();
$dias = array();
foreach($resul as $item)
{
$date = date_create($item->creation_date, timezone_open(Config::get('constants.zona_horaria')));
// Buenos Aires
date_timezone_set($date, timezone_open($empresa->zona_horaria));
$newDate = $date->format('d-m-Y G:i:s');
if ($this->check_in_range($request->input('from'), $request->input('to'), $newDate))
{
// ------------ resultados generales-------------//
$total++; //total mensajes
//mensajes enviado
if($item->type == 'IN'){
$in++; // total mensajes de salida
}else{
$out++; //totak de mensjaes de entrada
}
//-------------------------------------------//
//--------contar mensajes por dia-------------//
$fecha[] = $date->format('Y-m-d');
$hora[] = $date->format('G:i:s');
$fecha[] = 1;
foreach($fecha as $value)
{
if(isset($dias[$value]))
{
// si ya existe, le añadimos uno
$dias[$value]+=1;
}else{
// si no existe lo añadimos al array
$dias[$value]=1;
}
}
unset($fecha);//vaciamos el array
//---------------------------------------------//
}
}
// uasort($dias, [$this, 'Ordenar_Array']);
unset($dias['1']);
// var_dump($dias); exit;
// return array($total, $in, $out, $dias);
return view('informe/informe_general')
->with('total', $total)
->with('in', $in)
->with('out', $out)
->with('dias', $dias);
}
The json file:
[{
"id": "7797",
"number": "[YOUR NUMBER]",
"from": "[FROM NUMBER]",
"to": "[TO NUMBER]",
"type": "IN",
"text": "I need to know your pricing list",
"creation_date": "2017-03-18 14:49:23"
"process_date": "2017-03-18 14:49:23",
"custom_data": null
},
{
"id": "7798",
"number": "[YOUR NUMBER]",
"from": "[FROM NUMBER]",
"to": "[TO NUMBER]",
"type": "OUT",
"text": "Our pricing list is in our Web site http://my.beautiful-site.com!",
"creation_date": "2017-03-18 14:51:08"
"process_date": "2017-03-18 14:51:15",
"custom_data": "1234"
},
Here's what I want to get: