Skip to content
Advertisement

How to output random data from CSV in php?

I have a CSV file with 100 rows but want to output 5 of them at random like $data[0][1][2][3] and $data[2][2][3]

I got it to show but it shows in a group

JavaScript

Would like it to look like:

JavaScript

Advertisement

Answer

If you load the file into an array, it’s easy enough to come up with a list of “random” numbers, pull the corresponding line out, and use str_getcsv the same way you used fgetcsv in your code. It isn’t the most efficient way to do it, as you need to read the whole file into memory. But it’s necessary to determine the number of lines before choosing your random number.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement