Well, I suggest that I explain arrays a bit more. Let’s start by a simple one…
$array = array(“Ernie”, “Wiser3”, “someother person”);
As you see, there are three data entries in this array. since they were created using a standard PHP command, they are created something like this:
Key#0 Data “Ernie”
Key#1 Data “Wiser3”
Key#2 Data “someother person”
Now this is simple to understand. It is the basis of ALL arrays ever created. A numerical key and some sort of data. The key points at the data. To access your row of data, you would use $array[0]… Simple!
You can create arrays without numerical keys. You can use text for the keys if you need to. Such as this: $array = array(“Ernie”=>“somedata”, “Wiser3”=> "someotherdata’, “Etc”=>“moredata”);
To access this data, you can use something like $array[“Ernie”] to get my data values.
Those are the two basic arrays. Now, you can also have multidimensional arrays. These are where you have one array and the data is actually another array. They can be nested in different ways, but, that would be for another discussion.
With these explained, let’s go over your process step by step and figure out your code and data structure.
First, let’s get the file data itself. Your code:
$gamesToPick = 5; // Set limit for later
$f_contents = file("-games_list.txt"); // Load the file into a variable...
You used the FILE() function. This function will load the entire file into an array. ( No need to create the array first as the function will handle that. ) A few notes on this process. The text file needs to use normal formatting to be used. It must hold to the standards. If you created this file with a program of some sort, you need to insure it has standard formatting. I suggest using a smaller test file, with, let’s say 10 or 20 lines of data so you can verify it is correct. Maybe take your live data and just strip out the first lines for testing. Next, to insure that the FILE() function did what it was supposed to do, you can use this line to display it. You should see each array item with it’s key and data. And, that will give you an idea how the array looks in real life.
echo "<pre>" . print_r($f_contents, 1) . "</pre>";
Or you can use a var_dump, but, I like this version. Once you do this, it should show you what you are working with. Now, let’s talk about the random selection of 5 lines of this array. The ARRAY_RAND() function will do that for you as you already know. The way that this works is that it creates an array of keys from your array. Remember the key is an index. It points to data. Your line is:
$lines = $f_contents[array_rand($f_contents, $gamesToPick)];
This line will create a new array, in this case named $lines, from the list of keys in your live array.
Your live array will contain a list of lines from the text file. Each separated by standard line-returns.
( You can alter the separation rules, but, if your file is standard, it is not needed. ) Let’s say you use a 20 line sample to test with. The indexes (keys) would be 0 to 19. This ARRAY_RAND() function would grab five of these indexes and place them into an array. NOTE Remember, it creates an array of numbers which are indexes. This means it is like this sort of…
$lines = array(0=>rand-index1, 1=>rand-index2, 2...3...4=>rand-index5);
Just an example. You can use that display line to display this array also, so that you can understand it.
$echo "<pre>" . print_r($lines, 1) . "</pre>";
You can display any array this way when needed. Now, back to the newly created $lines array. It has five entries. These have indexes 0 to 4. (five total) And, the DATA for each of these are a randomly selected index from your $f_contents array.
Now, since this array of lines is a list of indexes, there is NO data attached to them. You would need to use them as pointers into the live data. Therefore, to access the live data, meaning the lines of text, you need to point to that data in this manner:
$the_data = $f_contents[ $lines[0] ] ; // Get the first, zero indexed value of data
$the_data = $f_contents[ $lines[4] ] ; // Get the last, fifth indexed value of data
The inside array name is the indexed array that only has KEYS inside it. Again, key => data.
The main data array, $f_contents[ ] contains the data that you need to use. Therefore, you have to use both arrays in this manner to acquire the data. Here is your original code example altered to work in this manner:
<?php
$gamesToPick = 5;
$f_contents = file("-games_list.txt");
$lines = $f_contents[array_rand($f_contents, $gamesToPick)];
for ($x = 0; $x < $gameToPick; $x++) {
$gameLine = $f_contents[ $lines[$x] ];
$gameLine = str_replace("^", "", $gameLine);
$gameLine = str_replace("|", "*", $gameLine);
echo $gameLine . "*";
}
?>
As you see, It is slightly different. I do not understand your displaying of the lines. It will display the lines of data separated with an asterisk. It may not include a linefeed. You might need to use a < BR > to get it to display on the screen correctly. I am not sure what you you want with the display as we have not seen your text file. ( Not needed, as you should understand the process now and can alter it as needed for your uses. )
Hopefully this will explain it all for you. Let us know if you have further questions…