Create array and count values

0

I'm having a hard time drawing a array in PHP with the following data coming from a JSON object, no, it does not come from a SQL query in a database.

Example of JSON :

[
{
    "data": {
        "ID": "349",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-22 12:46:35",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Antonio Carlos Braga"
    },
    "ID": 349,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "292",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-21 02:19:11",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Adriana Cristina Alves da Silva"
    },
    "ID": 292,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "196",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Facebook",
        "user_registered": "2018-03-20 14:28:58",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Adriana Fernandes de oliveira"
    },
    "ID": 196,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "286",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-21 01:19:53",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Alexandre Ramos de Campos"
    },
    "ID": 286,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "225",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-20 17:55:09",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Alex miguins de lima"
    },
    "ID": 225,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "318",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-21 14:35:38",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Alexandra Alves Ferreira"
    },
    "ID": 318,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "188",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-20 13:21:28",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Analucia c g pedreira"
    },
    "ID": 188,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "216",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Facebook",
        "user_registered": "2018-03-20 16:56:12",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "ANDREZA AP TOLEDO"
    },
    "ID": 216,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "299",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-21 05:39:16",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "angela maria de queiroz"
    },
    "ID": 299,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "291",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-21 02:15:56",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Angelica dos Santos Marques"
    },
    "ID": 291,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "224",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "user_registered": "2018-03-20 17:47:14",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Adilson Nicanor de Assis"
    },
    "ID": 224,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "190",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Facebook",
        "user_registered": "2018-03-20 13:39:58",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "António Carlos Bomfim Barros"
    },
    "ID": 190,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
},
{
    "data": {
        "ID": "359",
        "user_login": "###",
        "user_pass": "###",
        "user_nicename": "###",
        "user_email": "###",
        "user_url": "",
        "source": "Google",
        "user_registered": "2018-03-23 01:23:08",
        "user_activation_key": "",
        "user_status": "0",
        "display_name": "Marizete De Souza Lobato"
    },
    "ID": 359,
    "caps": {
        "subscriber": true
    },
    "cap_key": "gc_capabilities",
    "roles": [
        "subscriber"
    ],
    "allcaps": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "filter": null
}
]

That translated into a table would be:

| Fonte    | Data de cadastro |
|----------|------------------|
| Google   | 21/03/2018       |
| Google   | 22/03/2018       |
| Facebook | 21/03/2018       |
| Facebook | 22/03/2018       |
| Google   | 23/03/2018       |
| Facebook | 21/03/2018       |
| Google   | 21/03/2018       |
| Google   | 22/03/2018       |

Manually counting I know that on 21/03/2018 I had 2 Google signups and 2 Facebook signups, 03/22/2018 I had 1 Google signup and 1 Facebook signup, and so on

I'd like to generate a array that I believe to be associative to generate a table like this after a loop :

| Fonte    | Data de cadastro | Total de cadastros |
|----------|------------------|--------------------|
| Google   | 21/03/2018       | 2                  |
| Facebook | 21/03/2018       | 2                  |
| Facebook | 22/03/2018       | 1                  |
| Facebook | 22/03/2018       | 1                  |
| Google   | 23/03/2018       | 1                  |

I also know that this sum can be obtained with a function to add the values of array but I can not "draw" that array .

    
asked by anonymous 26.03.2018 / 15:54

1 answer

1

I'm not going to stick all the JSON .. pq would be a bit exaggerated, and the same is already in the question .. Here is an example code that separates the elements by 'source' and counts the number of times it occurred in each 'Ym-d' date.

  

The format of date format('Y-m-d') can be changed to format('d/m/Y') if desired.

$dados = json_decode([{...}]);
$result = [];
foreach($dados as $dado) {
  if(isset($dado->data->source)) { // Verifica se existe o 'source'
    $dataRegistro = (new DateTime($dado->data->user_registered))->format("Y-m-d");

    if(!isset($result[$dado->data->source])) { // Cria array no indice caso n exista.
      $result[$dado->data->source] = [];
    } 

    if(!isset($result[$dado->data->source][$dataRegistro])) { // Insere primeiro cadastro
      $result[$dado->data->source][$dataRegistro] = 1;
    } else {
      $result[$dado->data->source][$dataRegistro] += 1; // Adiciona cadastro existente..
    }
  }
}

print_r($result);

The output of this code will look something like this:

Array
(
    [Google] => Array
        (
            [2018-03-22] => 1
            [2018-03-21] => 1
        )

    [Facebook] => Array
        (
            [2018-03-20] => 1
        )

)

To print the 'Tabular' form values you could do this:

foreach($result as $k=>$v) {
  foreach($v as $data=>$ocorrencias) {
    echo $k . ' | ' . $data . ' | ' . $ocorrencias . "\n";
  }
}

What would result in:

Google | 2018-03-22 | 1
Google | 2018-03-21 | 1
Facebook | 2018-03-20 | 1
    
26.03.2018 / 16:36