Create array and count values

Asked

Viewed 70 times

0

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

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
}
]

What 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       |

Counting manually I know that on 21/03/2018 I had 2 Google registrations and 2 Facebook registrations, 22/03/2018 I had 1 Google registration and 1 Facebook registration, and so on.

I would like to generate one array which I believe is 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 sum the values of the array but I can’t "draw" that array.

  • Is that in the bank? tried to group? shows how the array is?

  • It comes in a JSON Object that I can easily in a loop and that I want to convert to array, but no, it doesn’t come from an SQL query

  • @Danilop.So could you paste this JSON that you receive? At least some of it (if it’s big enough) to see the format.

  • I updated the question ;)

  • Your JSON is not valid. Comma is missing and comma is wrong. Is this really what is coming from the bank ? Error to the decode none ? You’re putting the "true" $array = json_decode($json, true); ?

  • I had already cleaned the JSON so that it would not be full of useless information from Respse. This is the JSON answer via Postman, no result in PHP with json_decode()

  • You want to "group" the same records in the array, with a counter... that’s it ?

  • That, but separated by date. I thought about doing this through an array to generate a table at the end. Type, I get in each item of the JSON object: id, email, name, registration date, traffic source. And I need to compile in a table that on day X I received Y Google Signups, Z Facebook Signups.

  • According to this JSON.. not all records have the data you want.. Not all records have the specified Source.

  • This JSON is just an example, I can edit and put the source where it is missing. JSON is giant, I just edited the final result to get next to logic. I will edit again.

Show 5 more comments

1 answer

1


I am not going to paste all the JSON.. pq would get a little exaggerated, and the same is already found in the question.. Here is an example of code that separates the elements by 'source' and counts the number of times that occurred on each 'Y-m-d' date'.

The format of the date format('Y-m-d') may be changed to format('d/m/Y') if you wish.

$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 be something like this:

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

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

)

To print the values in 'Tabular' form you could do so:

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

Which would result in:

Google | 2018-03-22 | 1
Google | 2018-03-21 | 1
Facebook | 2018-03-20 | 1
  • It took me a while to test the code, but that’s exactly what I needed. I only found one difficulty in the part of the code you used POO, I changed it and it worked. Instead of $result[$dado->data->source][$dataRegistro] I used $result[$dado['data']['ID']][$dataRegistro] and I did, before print_r($result) came in white. Thanks man!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.