Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 12th, 2004, 09:01 PM
Registered User
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multidimensional Array help

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:
$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

@ $db = mysql_connect('','name','pass');

    if (!$db)
    echo 'Error:Could Not Connect';


$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'];
//would like to add more children here...

$xml = "<sirenreels>\n";

foreach ($playlist as $artist => $media)
$num_media = count($media);
       $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

Similar Threads
Thread Thread Starter Forum Replies Last Post
multidimensional array help for a newbie grahama PHP Databases 1 May 13th, 2008 09:41 AM
reading data from multidimensional array shish1 Beginning PHP 4 April 1st, 2007 06:42 PM
Multidimensional Array macsham Beginning PHP 2 February 24th, 2005 03:30 PM
Multidimensional array in ch9 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 November 2nd, 2004 04:44 PM
Efficient implementation of multidimensional array Utwig C# 2 October 26th, 2004 04:34 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.