470,810 Members | 880 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

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_object($result))) {
array_push($courses,$row); // push row object onto the array
}
echo "<p>$courses[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
Dec 20 '05 #1
2 1340
Rainman wrote:
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?


print "<p>{$courses[0]->name}</p>";
JW
Dec 20 '05 #2

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

Yep! That works! thanks!
Mark
Dec 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Gent | last post: by
34 posts views Thread by Christopher Benson-Manica | last post: by
11 posts views Thread by Soeren Sonnenburg | last post: by
8 posts views Thread by Derek Basch | last post: by
25 posts views Thread by Jack | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
5 posts views Thread by M. Fisher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.