php - 使用 PHP 访问对象的属性

标签 php arrays object braintree

我正在尝试使用 PHP 访问对象的一部分。我能够访问某些部分,但是,当我尝试访问包含信用卡号最后 4 位数字的部分时,它只会打印出 NULL。这是我的代码:

$customer = Braintree_Customer::find('51658462');
echo var_dump($customer);

这将打印出对象。这是打印出来的一部分:

object(Braintree_Customer)#1 (1) {
  ["_attributes"]=>
  array(14) {
    ["id"]=>
    string(8) "51658462"
    ["merchantId"]=>
    string(16) "tyn83x4j454dnvmt"
    ["firstName"]=>
    string(4) "Demo"
    ["lastName"]=>
    string(7) "Account"
    ["company"]=>
    NULL
    ["phone"]=>
    string(12) "555-555-5555"
    ["fax"]=>
    NULL
    ["website"]=>
    NULL
    ["createdAt"]=>
    object(DateTime)#8 (3) {
      ["date"]=>
      string(19) "2013-08-14 15:19:26"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["updatedAt"]=>
    object(DateTime)#9 (3) {
      ["date"]=>
      string(19) "2013-08-14 15:19:27"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["customFields"]=>
    string(3) "
  "
    ["creditCards"]=>
    array(1) {
      [0]=>
      object(Braintree_CreditCard)#2 (1) {
        ["_attributes"]=>
        array(29) {
          ["bin"]=>
          string(6) "371275"
          ["expirationMonth"]=>
          string(2) "01"
          ["expirationYear"]=>
          string(4) "2016"
          ["last4"]=>
          string(4) "1234"

我正在尝试访问 last4 属性。我尝试了几种不同的方法,但都不起作用。我认为 echo var_dump($customer->creditCards->last4); 会给我值,但事实并非如此。我错过了什么?

最佳答案

var_dump($customer->creditCards[0]->last4);

creditCards 是一个包含一项的数组 — 仅供引用,您不需要echo var_dump 的结果。

关于php - 使用 PHP 访问对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18241987/

相关文章:

C语言 : I want to see if a value of a[] is less than all the values of b[]

jquery - 如何在Jquery数组中添加DOM元素并循环它们?

java - 处理奇数和偶数元素的最有效方法是什么?

php - 回显数组按键 : Url and Caption

javascript - ReactJS/Javascript - 对象中项目的问题渲染组件

javascript - 组织对象以在需要时创建键和/或将不同的属性推送到值对数组

php - Laravel-5 如何使用 id 值和 name 值从数据库中填充选择框

php - 在不重定向到另一个页面的情况下回显错误并保留所有表单数据?

php - mysql: 1 INSERT INTO 查询两个结果

javascript - Laravel 5.2 从 URL 捕获 anchor 名称