• 0
Ricardo Infante

¿Cómo contar mensajes pendientes totales?

Question

Hola amigos cómo en PHP contar los menesajes totales que están sin leer en el Array?

 "user_id": "XXXXXX",
  "results": [
    {
      "order_id": "1645027493",
      "count": 1
    },
    {
      "order_id": "1645098715",
      "count": 1
    },
    {
      "order_id": "1645349894",
      "count": 1
    },
    {
      "order_id": "1647178750",
      "count": 1
    },
    {
      "order_id": "1647831741",
      "count": 1
    },
    {
      "order_id": "1649128303",
      "count": 1
    },
    {
      "order_id": "1650864771",
      "count": 2
    },
    {
      "order_id": "1650888741",
      "count": 1
    },
    {
      "order_id": "1654479486",
      "count": 2
    },
    {
      "order_id": "1655059414",
      "count": 1
    },
    {
      "order_id": "1655531672",
      "count": 2
    },
    {
      "order_id": "1655918367",
      "count": 1
    },
    {
      "order_id": "1656935777",
      "count": 3
    },
    {
      "order_id": "1657139232",
      "count": 1
    },
    {
      "order_id": "1657567298",
      "count": 1
    },
    {
      "order_id": "1657605506",
      "count": 1
    },
    {
      "order_id": "1657670101",
      "count": 1
    }
  ]
}

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

Hola Ricardo!

tienes que hacer algo asi:

function cuenta_mensajes($carry, $item)
{
    $carry += $item->count;
    return $carry;
}


$count = array_reduce($response["body]->results, "cuenta_mensajes");

echo $count;

 

Share this post


Link to post
Share on other sites
  • 0

Hola.

Tambien puedes hacer un foreach

Por ejemplo:

$results = $data['body']->results;

$count = 0;

foreach ($results as $order){

       $messages = $order->count;

        $count += $messages;

}

echo $count;

 

Saludos

Edited by Fernando Aguirre

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...