0

I have an array of values returned from Facebook - let's call it $array.

If I do print_r($array) - it looks like this:

Array
(
    [code] => 200
    [headers] => Array
        (
            [0] => Array
                (
                    [name] => Some value
                    [value] => *
                )

            [1] => Array
                (
                    [name] => Some value
                    [value] => Some value
                )

            [2] => Array
                (
                    [name] => Some value
                    [value] => Some value
                )


        )

    [body] => {"about":"Some more values.","can_post":true}
)

I need to extract the body part from this array.

I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.

Allen S
  • 3,471
  • 4
  • 34
  • 46

3 Answers3

3

$array->body would work if the variable $array was an object

For arrays, just use:

$body = $array['body'];

(see: http://be2.php.net/manual/en/language.types.array.php)

Olivier Kaisin
  • 519
  • 3
  • 12
2

If you want to access to your array via -> just do 1 more step:

$array = (object) $array;

And now, you can access to your body via:

$array->body;

Else without this step there is just one way:

$array['body'];


If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Community
  • 1
  • 1
Lkopo
  • 4,798
  • 8
  • 35
  • 60
1

Access array elements by using their name.

$array['body'];
sergserg
  • 21,716
  • 41
  • 129
  • 182