arrays of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rainman

    arrays of objects

    I'm having considerable trouble finding the proper syntax for accessing
    a specific element from an object which is in an array. Here is my
    pertinent code snippet:

    $courses = array(); // define empty array
    while(($row = mysql_fetch_obj ect($result))) {
    array_push($cou rses,$row); // push row object onto the array
    }
    echo "<p>$course s[0]->name</p>\n"; // access first object's name

    This does not give me the string I want, which is name of the first
    course in the array. I verified $courses[0] is an object. One of the
    members of the object is 'name'. How do I access the name of the first
    course?

    I tried:
    $courses[0]->name
    ${courses[0]}->name
    $courses[0]['name']
    ${courses[0]->name}

    and other combinations I could think of. Which way is the correct way?

    Mark
  • Janwillem Borleffs

    #2
    Re: arrays of objects

    Rainman wrote:[color=blue]
    > I tried:
    > $courses[0]->name
    > ${courses[0]}->name
    > $courses[0]['name']
    > ${courses[0]->name}
    >
    > and other combinations I could think of. Which way is the correct
    > way?[/color]

    print "<p>{$cours es[0]->name}</p>";


    JW


    Comment

    • Rainman

      #3
      Re: arrays of objects

      [color=blue]
      >
      > print "<p>{$cours es[0]->name}</p>";
      >
      >[/color]
      Yep! That works! thanks!
      Mark

      Comment

      Working...