How to get data from array in object

后端 未结 5 1257
情书的邮戳
情书的邮戳 2021-01-23 15:01

I can\'t seem to get specific data from an array inside an object.

$this->fields->adres gets the address correctly, but i can\'t get a level deeper.

相关标签:
5条回答
  • 2021-01-23 15:12

    You can also use type casting.

    $fields = (array) $this->data->fields;
    echo $fields['province'][0];
    
    0 讨论(0)
  • 2021-01-23 15:16
    $this->_data->fields['province'][0]
    
    0 讨论(0)
  • Fields and province are both arrays, you should be trying $this->fields["province"][0]

    0 讨论(0)
  • 2021-01-23 15:20

    As you can see by your output, object members are likely to be private (if you follow conventions, anyway you must prepend an underscore while calling them), so you're calling them the wrong way; This code works:

    $this->_data->fields['province'][0];
    

    You can see it in action here; I created a similar object, and using

    $membership = new RSMembershipModelSubscribe();
    echo $membership->_data->fields['province'][0];
    

    outputs "Flevoland" as expected.

    0 讨论(0)
  • 2021-01-23 15:34

    As fields is already an array, try this:

    $this->fields['province'][0]
    

    This assuming the [_data] object is $this.

    0 讨论(0)
提交回复
热议问题