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?

Community
  • 1
  • 1
sergserg
  • 21,716
  • 41
  • 129
  • 182

1 Answers1

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.

nickb
  • 59,313
  • 13
  • 108
  • 143