【问题标题】:Counting array in API JSON ResponseAPI JSON 响应中的计数数组
【发布时间】:2014-07-31 19:10:24
【问题描述】:

我正在尝试简单计算我的 Stripe 响应中有多少退款,但 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
    }
  }
}

【问题讨论】:

  • 计数对我有用...var_dump(count($string->data->object->refunds));
  • 谢谢@Farkie,我只是在做count($refund_array),而不是用var_dump()包裹它

标签: php arrays json stripe-payments


【解决方案1】:

这正是您尝试输出计数的方式。 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

【讨论】:

  • 哇,谢谢@Scrowler!为什么死了就不行了?
  • die 会根据参数是数字还是字符串而有所不同。
  • @scrowler Stripe 发布了新版本,在refunds Object 中的total_count 参数中给出了退款计数。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    根据2014-06-17 发布的新版 Stripe API,Refunds 对象被修改为 Charges 方法。

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

    data->object->refund->total_count

    【讨论】:

    • 感谢您让我知道更新。我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2014-12-22
    • 2017-09-17
    相关资源
    最近更新 更多