This is really a javascript question, and not a javascript forum.
Javascript can't query the server database, per se. Not without calling some page, which would have to be re-served in PHP's case.
I've read some articles on the collapsible stuff like you mention ... the arrays are populated on the original page load, unless they can be constructed by JS logic. It appears that in Friendster's case, they use Javascript to read an XML file on their server to populate the list. You can view their source. Pretty neat idea, but no PHP nor database involved, unless PHP or some similar technology is actually creating the XML files "on the fly".