Recursing data into a 2 dimensional array in PHP 5

Posted by user315699 on Stack Overflow See other posts from Stack Overflow or by user315699
Published on 2010-04-13T17:32:43Z Indexed on 2010/04/13 17:43 UTC
Read the original article Hit count: 298

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.

© Stack Overflow or respective owner

Related posts about php5

Related posts about arrays