Unable to sort array, and array keys don't increment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dbarak
Forum Newbie
Posts: 3
Joined: Wed May 07, 2008 10:18 pm

Unable to sort array, and array keys don't increment

Post by dbarak »

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

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.//
				}
			}
		}
	}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unable to sort array, and array keys don't increment

Post by requinix »

You "reset" $eventStuff every time you read a line. You aren't saving any of the previous lines. So the key will always be 0.

Your first goal is to get all the lines of all the files into one array, right? Watch this.

Code: Select all

$lines = array();
foreach (glob("event-titles/*") as $file) $lines = array_merge($lines, file($file));
Take a look at the functions: glob, array_merge, and file.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Unable to sort array, and array keys don't increment

Post by pickle »

Are you wanting to sort the contents of the file? Or sort the list of files?

A database really would be the best solution. Have you tried sqlite? Is that an option?

If you're stuck with a file, I have a couple suggestions. Store the contents either as a serialized array, or as a json string. Then, when you read the contents, you can just unserialize() or json_decode() the string and boom - you have an array.

If even that's not an option, at least look at the file() function, which reads a file in as an array with each line being a separate element - that might clean up your code a bit.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply