Re: Multidimensional Array advice from a newbie
| From: | Justin Patrin | Date: | Mon, 14 Jun 2004 16:53:18 +0000 |
| Subject: | Re: Multidimensional Array advice from a newbie | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-188231@lists.php.net to get a copy of this message | ||
Is this what you need? (See below for inline suggestions).
grahama@siren.cc wrote:
Hi :) I am creating an XML file out of a mysql query with nested arrays. Currently I can get 1 element and 1 child with a properly formatted XML file with the below script . My question is: How do I add 3 to 4 more child elements to the below 'playlist' array ? Currently ,I have one parent 'Artist' and one child 'english' ... I need to add more child elements to the 'Artist' array like urlPath, spanish, biography, etc My addled thoughts... So, would the multidimensional array be like:Instead, try $playlist[$row['artist_name']][] = array('english' => $row['english'],$playlist[ [$artist [ ] ] [$media[ ] ] [$mediaElement] ] ?for the 'trackName' child: $playlist [ "Artist 1" ] [ "Track 1" ] [ "trackName" ] or for 'urlPath' child : $playlist [ "Artist 1" ] [ "Track 1" ] [ "urlPath" ] Do I have to add another dimension to the 'playlist' array? Do I need another foreach loop ? Is there an easier more efficient way to do this? Be nice to spell out the schema in some way in the script...in case you need to add more levels...like a 'subCategory' I am a bit new to this so any help would be greatly appretiated .... head is spinning a bit <?php @ $db = mysql_connect('127.0.0.1','name','pass');if (!$db) { echo 'Error:Could Not Connect'; exit; } mysql_select_db('univision');$sql = 'SELECT artist.artist_name, media.english, media.path '; $sql .= 'FROM media, artist '; $sql .= 'WHERE artist.artist_id = media.artist_id LIMIT 0, 30 '; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $playlist[$row['artist_name']] [] = $row['english'];
'path' => $row['path'],
//add more here);
You could even add a sub-loop here:
$entry = array('english' => $row['english'],
'path' => $row['path'],
//add more here);
//guessing here
$sql = 'SELECT * FROM tracks WHERE artist = "'.$row['artist_name'].'"';
$sth = mysql_query($sql);
while($track = mysql_fetch_assoc($sth)) {
$entry['tracks'][$track['name']] = $track;
}
$playlist[$row['artist_name']][] = $entry;
//would like to add more children here... } $xml = "<sirenreels>\n"; foreach ($playlist as $artist => $media) { $num_media = count($media);-- paperCrane <Justin Patrin>$xml .= "<artist>\n"; $xml .= "\t<meta>\n"; $xml .= "\t\t<title>".$artist."</title>\n"; $xml .= "\t</meta>\n"; $xml .= "\t<content>\n";foreach ($media as $mediaVal) {$xml .= "\t\t<media>\n"; $xml .= "\t\t\t<english_name>".$mediaVal."</english_name>\n"; ///add more children ///add more children $xml .= "\t\t</media>\n"; } $xml .= "\t</content>\n"; $xml .= "</artist>\n"; }$xml .= "</sirenreels>\n"; print $xml