Add JSON array value in PHP

Asked

Viewed 47 times

0

Hello. I need to add all the arrays totalValue of this Json structure. In this case I wanted it to return 18897+13709 = 32,606

array(5) {
  ["list"]=>
  array(9) {
    **[0]**=>
    array(29) {
      ["orderId"]=>
      string(16) "10611855449-01"
      ["creationDate"]=>
      string(33) "2020-09-22T00:30:54.0000000+00:00"
      ["clientName"]=>
      string(12) "Guedes"
      ["items"]=>
      NULL
      **["totalValue"]**=>
      float(18897)
      ["paymentNames"]=>
      string(4) "Visa"
      ["status"]=>
      string(8) "invoiced"
      ["statusDescription"]=>
      string(8) "Faturado"
      ["marketPlaceOrderId"]=>
      NULL
      ["sequence"]=>
      string(6) "123456"
      ["salesChannel"]=>
      string(1) "1"
      ["affiliateId"]=>
      string(0) ""
      ["origin"]=>
      string(11) "Marketplace"
      ["workflowInErrorState"]=>
      bool(false)
      ["workflowInRetry"]=>
      bool(false)
      ["lastMessageUnread"]=>
      string(143) " --> 96 pedido nº 1063611855449-01 Pedido Entregue! Entrega realizada em Entregue Olá, . Obrigado por escolher a nossa loja. Seu produto já "
      ["ShippingEstimatedDate"]=>
      NULL
      ["ShippingEstimatedDateMax"]=>
      string(33) "2020-09-22T10:00:00.0000000+00:00"
      ["ShippingEstimatedDateMin"]=>
      string(33) "2020-09-22T10:00:00.0000000+00:00"
      ["orderIsComplete"]=>
      bool(true)
      ["listId"]=>
      NULL
      ["listType"]=>
      NULL
      ["authorizedDate"]=>
      string(33) "2020-09-22T00:31:08.0000000+00:00"
      ["callCenterOperatorName"]=>
      NULL
      ["totalItems"]=>
      int(22)
      ["currencyCode"]=>
      string(3) "BRL"
      ["hostname"]=>
      string(13) "asdfghjkklg"
      ["invoiceOutput"]=>
      array(1) {
        [0]=>
        string(5) "56070"
      }
      ["invoiceInput"]=>
      NULL
    }
    **[1]**=>
    array(29) {
      ["orderId"]=>
      string(16) "1063976954-01"
      ["creationDate"]=>
      string(33) "2020-09-21T23:49:37.0000000+00:00"
      ["clientName"]=>
      string(16) "Arimatsu"
      ["items"]=>
      NULL
      **["totalValue"]**=>
      float(13709)
      ["paymentNames"]=>
      string(9) "Card"
      ["status"]=>
      string(8) "invoiced"
      ["statusDescription"]=>
      string(8) "Faturado"
      ["marketPlaceOrderId"]=>
      NULL
      ["sequence"]=>
      string(6) "549845"
      ["salesChannel"]=>
      string(1) "1"
      ["affiliateId"]=>
      string(0) ""
      ["origin"]=>
      string(11) "Marketplace"
      ["workflowInErrorState"]=>
      bool(false)
      ["workflowInRetry"]=>
      bool(false)
      ["lastMessageUnread"]=>
      string(143) " --> 96 pedido nº 1063602976954-01 Pedido Entregue! Entrega realizada em Entregue Olá, . Obrigado por escolher a nossa loja. Seu produto já "
      ["ShippingEstimatedDate"]=>
      NULL
      ["ShippingEstimatedDateMax"]=>
      string(33) "2020-09-22T10:00:00.0000000+00:00"
      ["ShippingEstimatedDateMin"]=>
      string(33) "2020-09-22T10:00:00.0000000+00:00"
      ["orderIsComplete"]=>
      bool(true)
      ["listId"]=>
      NULL
      ["listType"]=>
      NULL
      ["authorizedDate"]=>
      string(33) "2020-09-22T11:20:17.0000000+00:00"
      ["callCenterOperatorName"]=>
      NULL
      ["totalItems"]=>
      int(14)
      ["currencyCode"]=>
      string(3) "BRL"
      ["hostname"]=>
      string(13) "efefqergwreg"
      ["invoiceOutput"]=>
      array(1) {
        [0]=>
        string(5) "56067"
      }
      ["invoiceInput"]=>
      NULL
    }

1 answer

0

Hello,

Utilize json_decode to get a PHP variable containing the values. From there, itere for the variable and sum all the elements:

   $meu_array = json_decode($json);

    $soma = 0;
    foreach($meu_array as $elementoAtual){
        $soma += $elementoAtual['totalValue'];
    }

This works for a json with N elements, but if it’s only 2, you can simplify:

   $soma = $meu_array[0]['totalValue'] + $meu_array[1]['totalValue']

Browser other questions tagged

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