0

我从 Go Cardless 网络挂钩收到以下响应,我正在努力解析它。下面的响应在一个名为 $item 的变量中

当我 var_dump($item) 我得到下面的内容..

object(GoCardlessPro\Resources\PayoutItem)#87 (6) {
    ["model_name":protected]=>
    string(10) "PayoutItem"
    ["amount":protected]=>
    string(6) "2800.0"
    ["links":protected]=>
    object(stdClass)#44 (1) {
      ["payment"]=>
      string(14) "PM0020P6VREV7P"
    }
    ["type":protected]=>
    string(16) "payment_paid_out"
    ["data":"GoCardlessPro\Resources\BaseResource":private]=>
    object(stdClass)#49 (4) {
      ["type"]=>
      string(16) "payment_paid_out"
      ["amount"]=>
      string(6) "2800.0"
      ["taxes"]=>
      array(0) {
      }
      ["links"]=>
      object(stdClass)#44 (1) {
        ["payment"]=>
        string(14) "PM0020P6VREV7P"
      }
    }
    ["api_response"]=>
    NULL
  }
  [1]=>
  object(GoCardlessPro\Resources\PayoutItem)#88 (6) {
    ["model_name":protected]=>
    string(10) "PayoutItem"
    ["amount":protected]=>
    string(5) "-10.0"
    ["links":protected]=>
    object(stdClass)#39 (1) {
      ["payment"]=>
      string(14) "PM0020P6VREV7P"
    }
    ["type":protected]=>
    string(7) "app_fee"
    ["data":"GoCardlessPro\Resources\BaseResource":private]=>
    object(stdClass)#42 (4) {
      ["type"]=>
      string(7) "app_fee"
      ["amount"]=>
      string(5) "-10.0"
      ["taxes"]=>
      array(0) {
      }
      ["links"]=>
      object(stdClass)#39 (1) {
        ["payment"]=>
        string(14) "PM0020P6VREV7P"
      }
    }
    ["api_response"]=>
    NULL
  }
  [2]=>
  object(GoCardlessPro\Resources\PayoutItem)#89 (6) {
    ["model_name":protected]=>
    string(10) "PayoutItem"
    ["amount":protected]=>
    string(5) "-58.0"
    ["links":protected]=>
    object(stdClass)#46 (1) {
      ["payment"]=>
      string(14) "PM0020P6VREV7P"
    }
    ["type":protected]=>
    string(14) "gocardless_fee"
    ["data":"GoCardlessPro\Resources\BaseResource":private]=>
    object(stdClass)#32 (4) {
      ["type"]=>
      string(14) "gocardless_fee"
      ["amount"]=>
      string(5) "-58.0"
      ["taxes"]=>
      array(1) {
        [0]=>
        object(stdClass)#41 (6) {
          ["amount"]=>
          string(4) "10.0"
          ["currency"]=>
          string(3) "GBP"
          ["destination_amount"]=>
          string(4) "10.0"
          ["destination_currency"]=>
          string(3) "GBP"
          ["exchange_rate"]=>
          NULL
          ["tax_rate_id"]=>
          string(8) "GB_VAT_1"
        }
      }
      ["links"]=>
      object(stdClass)#46 (1) {
        ["payment"]=>
        string(14) "PM0020P6VREV7P"
      }
    }
    ["api_response"]=>
    NULL
  }
}

当我 var_export($item[2]) 我得到下面的内容..

GoCardlessPro\Resources\PayoutItem::__set_state(array(
   'model_name' => 'PayoutItem',
   'amount' => '-58.0',
   'links' => 
  (object) array(
     'payment' => 'PM0020P6VREV7P',
  ),
   'type' => 'gocardless_fee',
   'data' => 
  (object) array(
     'type' => 'gocardless_fee',
     'amount' => '-58.0',
     'taxes' => 
    array (
      0 => 
      (object) array(
         'amount' => '10.0',
         'currency' => 'GBP',
         'destination_amount' => '10.0',
         'destination_currency' => 'GBP',
         'exchange_rate' => NULL,
         'tax_rate_id' => 'GB_VAT_1',
      ),
    ),
     'links' => 
    (object) array(
       'payment' => 'PM0020P6VREV7P',
    ),
  ),
   'api_response' => NULL,
))

现在我正在寻找访问 'taxes' => array ( 0 => (object) array('destination_amount' => '10.0', 我试过 echo $item[2]->taxes->destination_amount; echo $ item[2]->taxes['destination_amount]; 和其他各种迭代,但都无济于事。我可以看到我正在处理对象和数组但是我认为我知道的只是不起作用。任何人都可以为我指明正确的方向,因为此刻,我只见树木不见森林。

4

1 回答 1

0

每个的taxes属性PayoutItem是一个税项数组。

因为taxes是数组,taxes->destination_amount不代表什么!

您可能想要获取destination_amount税数组中特定项目的 ,例如:

echo $item[2]->taxes[0]->destination_amount;

或遍历所有税收项目并对每个项目做一些事情:

foreach ($item[2]->taxes as $tax) {
  echo $tax->amount . PHP_EOL;
}
于 2020-11-27T11:54:10.943 回答