计算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
    }
  }
}

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

The count() just needs to be cast as a string:

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

As per new version of Stripe API which was released on 2014-06-17, Refunds object is modified for Charges Method.

Now you can directly get refund count using total_count parameter in refunds object.

data->object->refund->total_count