0

Possible Duplicate:
Mixed Array and object

I'm using print_r to see what the array contains:

Array ( 
    [2] => stdClass Object ( 
        [id] => 2 
        [category] => 1 
        [sortorder] => 10001 
        [shortname] => 2323 
        [fullname] => asdaSDa 
        [startdate] => 1343188800 
        [visible] => 1 
        [groupmode] => 0 
        [groupmodeforce] => 0 
        [numsections] => 10 
        [role] => student 
        [rolename] => Student 
    ) 
)

I'd like to retrive the value of [id]. How can achieve this using PHP?

I've tried the following but receive an exception 500 from the server:

echo "<h1>CODIGO: ".$courses[2]["id"]."</h1>";

Any suggestions?

1
  • hmm shouldn't it be like $courses[2]->id ? Commented Aug 28, 2012 at 14:16

1 Answer 1

8

What you have is an array of objects, so you can't access id with brackets . Instead, you need to use -> to get the object property directly:

echo $array[2]->id;

If the key changes (but the element is the first), use array_shift():

$first = array_shift( $array);
echo $first->id;

Similarly, use array_pop() if the element is the last in the array.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.