dtvpl739577 2014-06-10 21:08
浏览 55
已采纳

计算API JSON响应中的数组

I'm trying to do a simple count of how many refunds are in my Stripe Response but count() isn't working and I don't really know any other way of achieving this.

Could anyone point me in the right direction?

$retrieve_event =  Stripe_Event::retrieve("evt_00000000000000");
$event_json_id = json_decode($retrieve_event);

$refund_array = $event_json_id->{'data'}->{'object'}->{'refunds'};
die(count($refund_array));

This is the response of $retrieve_event

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "charge.refunded",
  "object": "event",
  "request": null,
  "data": {
    "object": {
      "id": "ch_00000000000000",
      "object": "charge",
      "created": 1402433517,
      "livemode": false,
      "paid": true,
      "amount": 1000,
      "currency": "usd",
      "refunded": true,
      "card": {
        "id": "card_00000000000000",
        "object": "card",
        "last4": "0028",
        "type": "Visa",
        "exp_month": 8,
        "exp_year": 2015,
        "fingerprint": "a5KWlTcrmCYk5DIYa",
        "country": "US",
        "name": "First Last",
        "address_line1": "null",
        "address_line2": null,
        "address_city": "null",
        "address_state": "null",
        "address_zip": "null",
        "address_country": "US",
        "cvc_check": null,
        "address_line1_check": "fail",
        "address_zip_check": "pass",
        "customer": "cus_00000000000000"
      },
      "captured": true,
      "refunds": [
        {
          "id": "re_104CKt4uGeYuVLAahMwLA2TK",
          "amount": 100,
          "currency": "usd",
          "created": 1402433533,
          "object": "refund",
          "charge": "ch_104CKt4uGeYuVLAazSyPqqLV",
          "balance_transaction": "txn_104CKt4uGeYuVLAaSNZCR867",
          "metadata": {}
        },
        {
          "id": "re_104CKt4uGeYuVLAaDIMHoIos",
          "amount": 200,
          "currency": "usd",
          "created": 1402433539,
          "object": "refund",
          "charge": "ch_104CKt4uGeYuVLAazSyPqqLV",
          "balance_transaction": "txn_104CKt4uGeYuVLAaqSwkNKPO",
          "metadata": {}
        },
        {
          "id": "re_4CL6n1r91dY5ME",
          "amount": 700,
          "currency": "usd",
          "created": 1402434306,
          "object": "refund",
          "charge": "ch_4CL6FNWhGzVuAV",
          "balance_transaction": "txn_4CL6qa4vwlVaDJ"
        }
      ],
      "balance_transaction": "txn_00000000000000",
      "failure_message": null,
      "failure_code": null,
      "amount_refunded": 1000,
      "customer": "cus_00000000000000",
      "invoice": null,
      "description": "this is a description",
      "dispute": null,
      "metadata": {},
      "statement_description": "this is a description",
      "fee": 0
    }
  }
}
  • 写回答

3条回答 默认 最新

  • dongzhong7299 2014-06-10 21:16
    关注

    It's just the way you're trying to output the count. This example outputs 3:

    echo count($refund_array);
    exit;
    

    Whereas this example doesn't:

    die(count($refund_array));
    

    The reason is because you're simply passing in an integer into die(). From the manual:

    If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

    This example works because the message is a string:

    die('Count: ' . count($refund_array)); // Count: 3
    

    ...or:

    die((string) count($refund_array));    // 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器