计算API JSON响应中的数组

时间:2014-06-10 21:08:10

标签: php arrays json stripe-payments

我试图简单计算条纹响应中有多少退款,但count()不起作用,我真的不知道实现此目的的任何其他方法

有人能指出我正确的方向吗?

$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));

这是$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 个答案:

答案 0 :(得分:2)

这就是你尝试输出计数的方式。 This example outputs 3

echo count($refund_array);
exit;

Whereas this example doesn't

die(count($refund_array));

原因是因为您只是将整数传入die()From the manual

  

如果status是一个整数,该值将用作退出状态而不打印。退出状态应在0到254范围内,退出状态255由PHP保留,不得使用。状态0用于成功终止程序。

This example有效,因为该消息是一个字符串:

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

...或:

die((string) count($refund_array));    // 3

答案 1 :(得分:1)

count()只需要转换为字符串:

die((string) count($refund_array));

答案 2 :(得分:1)

根据2014-06-17上发布的新版Stripe API,针对Charges方法修改了退款对象。

现在,您可以使用退款对象中的total_count参数直接获取退款计数。

  

DATA->对象 - >退还的款项> TOTAL_COUNT