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.

1
  • -> is for objects, you have array Commented Feb 23, 2014 at 14:31

3 Answers 3

3

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

For arrays, just use:

$body = $array['body'];

(see: https://www.php.net/manual/en/language.types.array.php)

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

1

Access array elements by using their name.

$array['body'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.