homepage Welcome to WebmasterWorld Guest from 54.234.225.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Recursing data into a 2 dimensional array
For each file, populate with result of metadata function
digitaltoast




msg:4115236
 5:33 pm on Apr 13, 2010 (gmt 0)

I'm getting bamboozled by "for each" loops and two dimensional arrays, and I'm a php newb so please bear with me (and ignore any variables with the word "image" - it's all about the mp3s, I just didn't change it from the xml tutorial)

I found a php function on the net that list files in a directory, the output of which is:

Array
(
[0] => audio/1.mp3
[1] => audio/2.mp3
[2] => audio/3.mp3
[3] => audio/4.mp3
[4] => audio/5.mp3
)

As expected.

And another that lists some info about mp3 files.

$mp3datafile = 'audio/1.mp3';
$m = new mp3file($mp3datafile);
$mp3dataArray = $m->get_metadata();
print_r($mp3dataArray);
unset($mp3dataArray);


The output of which is

Array
(
[Filesize] => 31972
[Encoding] => CBR
[etc]
)


In order to automatically build RSS for a podcast, I need to generate XML for each item. So far so good.

This is how I'm making the xml

foreach ($imagearray as $key => $val) {
$tnsoundfile = $xml_generator->addChild('item');
$tnsoundfile->addChild('title', $podcasttitle);
$enclosure = $tnsoundfile->addChild('enclosure');
$enclosure->addAttribute('url', $val); // that's the filename
$enclosure->addAttribute('length', $mp3dataArray[Filesize]); // << Length is file length, not time. But later I also need $mp3dataArray[Length mm:ss] for duration tag.
$enclosure->addAttribute('type', 'audio/mpeg');
$tnsoundfile->addChild('guid', $path_to_image_dir.'/'.$val);
}


(The above has been truncated, I realise it's not proper xml right now, but it was just to show what was going on).

Perfect. But I need to do it for as many files as there are in the directory.

So, I have an array of the names of the files in the directory in $mp3data
And, I have an array of mp3 data in $mp3dataArray from one iteration of the get_metadata() function.

If I do the following, then I get a nice list of the mp3 data of the 5 files in the directory:

foreach ($mp3data as $key => $val) {
$mp3datafile = $val;
$m = new mp3file($mp3datafile);
$mp3dataArray = $m->get_metadata();
print_r($mp3dataArray);
unset($mp3dataArray);
}


As expected.

Where I'm struggling, and have been for most of the day in spite of reading many forums and tutorials, is how to populate the "second dimension" of the array, so that it goes through 1,2,3,4 and 5.mp3 (or however many there are), extracts the metadata, then allows me to use it in the xml section above.

Here's what I have

foreach ($mp3data as $key => $val) {
$mp3datafile = $val;
$m = new mp3file($mp3datafile);
$mp3dataArray = $m->get_metadata();
$mp3testarray = array($mp3dataArray);
}
print_r($mp3dataArray);


Shouldn't that line print_r($mp3dataArray); give me a nice list of 5 lots of mp3 data, in the way it did when I recursed through the loop as before?

Cos this is driving me nuts! It must be something so simple, and any help would be greatly appreciated.

Thank you in advance.

 

CyBerAliEn




msg:4115248
 5:43 pm on Apr 13, 2010 (gmt 0)

For your "debugging"... try changing this:
foreach ($mp3data as $key => $val) {
$mp3datafile = $val;
$m = new mp3file($mp3datafile);
$mp3dataArray = $m->get_metadata();
$mp3testarray = array($mp3dataArray);
}
print_r($mp3dataArray);


To...
$allresults = array();
foreach ($mp3data as $key => $val) {
$mp3datafile = $val;
$m = new mp3file($mp3datafile);
$mp3dataArray = $m->get_metadata();
$allresults[] = $mp3dataArray;
}
print_r($allresults);


This assumes your "mp3data" variable is an array in the form you have at the top of your post (just a list of files).

What does it do? (step-by-step)

<?php
//This creates an array variable (empty):
$allresults = array();

//This starts a loop, running through each item inside array mp3data; each loop executes the code inside the braces:
foreach ($mp3data as $key => $val)
{

//This takes the value of the array item (the file path/name) and set it as the "mp3 file":
$mp3datafile = $val;

//This creates an instance of 'mp3file' using the mp3 file:
$m = new mp3file($mp3datafile);

//This creates an array of MP3 data using the mp3file object for the current mp3 file:
$mp3dataArray = $m->get_metadata();

//This "adds" the current mp3 data array onto the end of the array "allresults":
$allresults[] = $mp3dataArray;

}

//This prints out (to screen) in a human-friendly-readable form, all of the contents within variable (array) allresults:
print_r($allresults);

?>



Though this code (and yours?) doesn't actually do anything functional... this should show you how to achieve what you're after.

CyBerAliEn




msg:4115256
 5:57 pm on Apr 13, 2010 (gmt 0)

Some further points to note, regarding your originail code (FYI):

$mp3testarray = array($mp3dataArray);
^This doesn't do as you anticipate. To create an empty array, specify as "array()". If you want to populate an array, it must be in form:
array('key1'=>'value1','key2'=>'value3',[etc])

Your code is trying to create an array (mp3testarray) using another array (mp3dataArray) as "input" --- I don't believe this is valid (or works as wanted).


Also:
print_r($mp3dataArray);
^This is going to print the array of "mp3dataArray". Note that this variable is defined each time the loop runs. This means that each time the FOREACH loop runs, the value of this variable is re-defined. So when you go to use/access this variable AFTER the end of the loop, you'll only get the LAST value of 'mp3dataArray'.

It appears you want to get ALL of the data arrays together for all of your MP3s in order to do the XML/RSS output.

Assuming you collect all of your MP3s into "allresults"... all results will look like (using dummy data):
Array
(
[0] => Array
(
[title] => some title
[bitrate] => 128
[length] => 12:38
)

[1] => Array
(
[title] => some title
[bitrate] => 128
[length] => 12:38
)

[2] => Array
(
[title] => some title
[bitrate] => 128
[length] => 12:38
)

[3] => Array
(
[title] => some title
[bitrate] => 128
[length] => 12:38
)

[4] => Array
(
[title] => some title
[bitrate] => 128
[length] => 12:38
)

)


The "allresults" array is a collection of all your individual arrays of mp3 data (for each song). Clearly, it is multi-dimensional.

Now suppose you want access/display/use the data (for each file)... play with code such as:
<?php
//assumes 'allresults' or such is already defined/valued
foreach ($allresults as $mp3data)
{
$title = $mp3data['title'];
$bitrate = $mp3data['bitrate'];
$length = $mp3data['length'];
echo "<div>MP3 Song - Title($title) BitRate($bitrate) Length($length)</div>";
}
?>


Using the above snippet... should give you an idea of how to loop through the resulting array to build out your XML/RSS. Basically, each "entry" of "allresults" IS an array itself. So if you loop through "allresults", each item within the loop is an array, and it is an array of your song's data.


print_r() Note
To see the output of "print_r" in a friendly format (via HTML in your browser), try:
<?php echo '<pre>';print_r($someArray);echo '</pre>';

Wrapping the output in "pre" tags will have HTML display the newline characters properly.

digitaltoast




msg:4115291
 6:35 pm on Apr 13, 2010 (gmt 0)

I'm impressed! A great tutorial in itself. Clearly a lot to learn.

I'm not going to try this tonight as my head is fried, but I wanted to thank you, and I'm going to print it out and work through it.

I think part of the problem is that I've been hacking stuff together from tutorials, modifying them slightly, and so far by luck, fluke and miracle, it's sort of worked. Arrays and loops are one of those things I clearly need to understand in more depth before messing with them!

Thanks again, I'll change my code tomorrow and report back.

digitaltoast




msg:4116256
 10:03 am on Apr 15, 2010 (gmt 0)

Just wanted to come back and thank you again, worked great.
Wish I could somehow "rate you up", but never mind, have some karma or kudos!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved