Need help getting random lines from file into array then display them from array

Topic says what i’m trying to do. Didn’t think anything here was beyond my abilities, but i’ve been stuck on this for far too long. Thanks for your time and help.

<?php $gamesToPick = 5; $f_contents = file("-games_list.txt"); $lines = $f_contents[array_rand($f_contents, $gamesToPick)]; for ($x = 0; $x < $gameToPick; $x++) { $gameLine = str_replace("^","
",$lines($x)); $gameLine = str_replace("|","",$gameLine); $gameLine .= ""; echo $gameLine; } ?>

Please use code tags or three back-ticks at beginning and end of your code. This makes it easier to for us to copy and test and to help you. Thanks!

<?php
 $gamesToPick = 5;
 $f_contents = file("-games_list.txt");
 $lines = $f_contents[array_rand($f_contents, $gamesToPick)];
 for ($x = 0; $x < $gameToPick; $x++) {
     $gameLine = str_replace("^","",$lines($x)); 
     $gameLine = str_replace("|","*",$gameLine);
     $gameLine .= "*";
     echo $gameLine;
 } 
?>

With this code, you are picking an array of five “ARRAY KEYS”. Not the data. Therefore, you need to get those using $f_contents[the-key] … You are pulling them from $lines which is just the KEYS.
You would need to either pull the data out and then run your string replaces on it or use the correct key instead. $gameLine = str_replace("^", “”, $f_contents[$lines($x)] ); See the difference?

You just need to remember that the array_rand selects random keys not data. Hope this helps!

Thanks ErnieAlex, now i understand what’s wrong, i need to figure out a way to fix it.
Is there an alternative to $f_contents that picks as data?
Could i convert the keys to data?
Maybe i should restructure the data in -games_list.txt
Tomorrow is another day, so much to learn and never seems to be enough time to learn it.

Well, all arrays have a key and data. The key is the index pointing to the data.
So, your routine will work as is if you just change it like I showed you. Your $x is key not data.
What I am saying is change your two $gameLine = lines to index into the data and it should work.

Tomorrow, I can do more with your example if you wish. 1 AM here now. Your code should work,
if you change it so you use the data… Not just the keys…

Clearly there is still something i don’t understand about the differences of working with array keys and array data. I’ve had similar issues getting arrays to work before but in past i always eventually got it working. Still haven’t gotten this to work. While i think i understand the concepts i don’t fully understand the syntax differences.

In this line of code $lines is an array, but $gameLine stays a regular variable, right?
/// $gameLine = str_replace("^","
",[$lines($x)]);

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…

Hmmm… still not working. Been using print_f to look at the arrays as i try different things.

f_content looks good. Here’s a piece:

f_contents Array
(
[0] => 688 Attack Sub^“688 Attack Sub aims to bring the big bang quite a lot closer with scenarios ranging from evading Soviet ships to nuking large sections of East Germany. Who says the Cold War’s over? However, despite all the tension, this is not a game of quick thrills.
Just as in real war there are long periods with nothing much to do. It’s quite possible to play the game for a couple of hours and only touch the computer half a dozen times. Game speed can be increased but this is probably a game to the hardened sim enthusiast.
|(ZERO 9/1990)”^

[1] => Abuse^"Abuse is a run and gun video game developed by Crack dot Com and published by Electronic Arts in North America and Origin Systems in Europe.

You are Nick Vrenna, falsely locked inside a high-security prison. The genetic experiments conducted in the facility, combined with a full-scale riot that took place recently, caused all guards and inmates to be transformed into insane, aggressive mutants. Your job is to escape.
|(Wikipedia)"^

The lines array however stays empty. So i think the problem is here:

$lines = $f_contents[array_rand($f_contents, $gamesToPick)];

I’m doing some research and trying to figure out why.

OMG! Abuse! I loved that game! I forgot about it… Oldie for sure!!! Ha! Thanks for the long time memory ! 688 Attack Sub too, but, I didn’t like that one much… Dates me for sure!

Well, I screwed up typing so fast! That line is in error. I had two versions and somehow combined them.
Very sorry for that! It should be:

$lines = array_rand($f_contents, $gamesToPick);

Again, sorry for the mistype!

Sponsor our Newsletter | Privacy Policy | Terms of Service