Pregunta

I am attempting to access part of an object with PHP. I am able to access certain parts, however, when I try accessing a part that contains the last 4 digits of a credit card number, it just prints out NULL. Here is my code:

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

This will print out the Object. Here is a piece of the print out:

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"

I am trying to access the last4 attribute. I have tried several different methods but none of them work. I thought that echo var_dump($customer->creditCards->last4); would give me the value but it does not. What am I missing?

¿Fue útil?

Solución

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

creditCards is an array containing one item — and FYI, you don’t need to echo the results of a var_dump.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top