Unable to sort array, and array keys don't increment
Posted: Mon Apr 30, 2012 5:51 pm
Hello,
I'm having a heck of a time with a PHP issue, and I have a feeling it's easy but...
I'm a little experienced with PHP, but not confident with it. I'm working in a bit of a weird server environment, but the short story is that I have no database access, so stored data has to be in text files. Anyway, what I need to do is look in a directory for text files. Then I have to open each file and display the text in it. The displayed results need to be sorted numerically. I tried sorting based on file name, but then I decided to embed the file name in the file itself, and I'd like to try to sort on that.
However, I'm able to display the file names of the files in the directory, and I can sort those numerically, OR I can open the files and display the contents, but I can't sort the results. In the course of trying to get this to work, I decided to echo the array key (correct term?) and the data itself. What's odd is that the key always shows up as 0 (zero), but I'm still able to read and echo the content of each file. So the "foreach" IS cycling through the files, but the array key is staying the same. I haven't found a way to actually get that to increment. I believe that's necessary for sorting, correct?
I've stripped the code below to the absolute minimum. It may not be correct (I chopped out a bunch of stuff while trying to debug it), but it IS returning the all items in the array (one item from each text file). Can anyone help? I've spent many hours trying to get this to work, and I just can't figure out what I'm doing wrong.
Dave
I'm having a heck of a time with a PHP issue, and I have a feeling it's easy but...
I'm a little experienced with PHP, but not confident with it. I'm working in a bit of a weird server environment, but the short story is that I have no database access, so stored data has to be in text files. Anyway, what I need to do is look in a directory for text files. Then I have to open each file and display the text in it. The displayed results need to be sorted numerically. I tried sorting based on file name, but then I decided to embed the file name in the file itself, and I'd like to try to sort on that.
However, I'm able to display the file names of the files in the directory, and I can sort those numerically, OR I can open the files and display the contents, but I can't sort the results. In the course of trying to get this to work, I decided to echo the array key (correct term?) and the data itself. What's odd is that the key always shows up as 0 (zero), but I'm still able to read and echo the content of each file. So the "foreach" IS cycling through the files, but the array key is staying the same. I haven't found a way to actually get that to increment. I believe that's necessary for sorting, correct?
I've stripped the code below to the absolute minimum. It may not be correct (I chopped out a bunch of stuff while trying to debug it), but it IS returning the all items in the array (one item from each text file). Can anyone help? I've spent many hours trying to get this to work, and I just can't figure out what I'm doing wrong.
Dave
Code: Select all
<?php
if ($handle = opendir('./event-titles'))
{
while (false !== ($fileName = readdir($handle)))
{
if ($fileName != "." && $fileName != "..")
{
$openedFile=fopen("event-titles/".$fileName,"r");
while(!feof($openedFile))
$fileContent = fgets($openedFile);
$eventStuff = array($fileContent);
{
foreach ($eventStuff as $key => $val)
echo $key."<br/>";//Returns "0" (zero), does not increment.//
echo $val."<br/>";//Correctly returns each element in array.//
}
}
}
}
?>