• 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now