Create an array with date and time data in php

3

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:

    
asked by Gonzalo Sebastian Ulloa Astorg 22.10.2018 в 03:07
source

1 answer

1

I think I understand what you want. Starting from the original JSON, you should create a preliminary matrix, with the data "raw", with json_decode (). Then, it is a matter of going through it, and checking if there are keys with each date and time, in a matrix, let's say, processed.

The following code shows what I suggest:

<?php
    $datosOriginales = <<<JSON
        [{
            "id": "7797",
            "number": 949493394,
            "from": 944939,
            "to": 494030303030,
            "type": "IN",
            "text": "Texto de prueba",
            "creation_date": "2017-03-18 14:49:23",
            "process_date": "2017-03-18 14:49:23",
            "custom_data": null
        },
        {
            "id": "4988",
            "number": 54455,
            "from": 944939,
            "to": 5666589,
            "type": "OUT",
            "text": "Texto de prueba",
            "creation_date": "2018-04-18 08:49:23",
            "process_date": "2018-04-18 08:49:23",
            "custom_data": null
        }]      
JSON;

    $matrizOriginal = json_decode($datosOriginales, true);
    $matrizProcesada = [];
    foreach ($matrizOriginal as $item) {
        $fecha = date("d-m-Y", strtotime($item["creation_date"]));
        if (!array_key_exists($fecha, $matrizProcesada)) {
            $matrizProcesada[$fecha] = [];
        }
        $hora = date("H", strtotime($item["creation_date"])).":00";
        if (!array_key_exists($hora, $matrizProcesada[$fecha])) {
            $matrizProcesada[$fecha][$hora] = [
                "totales"=>0,
                "IN"=>0,
                "OUT"=>0
            ];
        }
        $matrizProcesada[$fecha][$hora]["totales"] ++;
        if ($item["type"] == "IN") {
            $matrizProcesada[$fecha][$hora]["IN"] ++;
        } else {
            $matrizProcesada[$fecha][$hora]["OUT"] ++;
        }
    }

    echo "<pre>";
    var_dump($matrizOriginal, $matrizProcesada);
    echo "</pre>";

?>

If I have understood your question, that code can solve the issue. If that is not what you were referring to, please specify a little more, and I will try to help you.

PD. As a suggestion, the texts of the matrix you are looking for and the JSON that you are part of, put them as text, not as images. This makes it easier to work on.

    
answered by 22.12.2018 в 22:58