1

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.

I've tried:

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

And: (edit)

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

But it does not return anything while it should return "Flevoland".

First part of the object print_r($this, TRUE) below:

RSMembershipModelSubscribe Object
(
    [_id] => 2
    [_extras] => Array
        (
        )

    [_data] => stdClass Object
        (
            [username] => testzz
            [name] => testzz
            [email] => xxxx@example.com
            [fields] => Array
                (
                    [province] => Array
                        (
                            [0] => Flevoland
                        )

                    [plaats] => tesdt
                    [adres] => test
Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
Antoon Cusell
  • 11
  • 1
  • 1
  • 4

5 Answers5

6

You can also use type casting.

$fields = (array) $this->data->fields;
echo $fields['province'][0];
Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28
4

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.

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • 1
    You were almost there, @Antoon :). Please consider marking this answer as "accepted" (the tick mark under the vote count) for future references; glad to have helped :) – Damien Pirsy Oct 28 '11 at 19:02
0
$this->_data->fields['province'][0]
r15habh
  • 1,468
  • 3
  • 19
  • 31
0

As fields is already an array, try this:

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

This assuming the [_data] object is $this.

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
0

Fields and province are both arrays, you should be trying $this->fields["province"][0]

Whetstone
  • 1,199
  • 8
  • 8
  • Thanks for your quick response guys, unfortunately, none of them gave back any result.. any other suggestions? – Antoon Cusell Oct 28 '11 at 18:20
  • That really should have done it. Can you do a print_r on $this directly before running this line of code, then run it after? I'm suspecting there may be a different problem. – Whetstone Oct 28 '11 at 19:27