young_coder Posted August 15, 2010 Share Posted August 15, 2010 Dear all, Can somebody help me please? Instead of ‘username’ and 'password', I need random values from CSV file. Can somebody show me how can I do this? My CSV file looks like this: user001,userpass001 user002,userpass002 user003,userpass003 My script looks like this: <?php $t= new post(); $t->username='username'; $t->password='password'; $res = $t->update('This is some text.'); ?> Thank you very much advance. Link to comment https://forums.phpfreaks.com/topic/210773-random-username-and-password-from-csv-file/ Share on other sites More sharing options...
Floydian Posted August 15, 2010 Share Posted August 15, 2010 Hello young_coder, You can use the file() function to read the csv file into an array. You have line breaks in your csv file, and each line will be an element in the array. Then you can use shuffle() to randomize the order of the array, and reset() to pluck off the first element of that array. Lastly, you would call explode() to split the username and password. Cheers Link to comment https://forums.phpfreaks.com/topic/210773-random-username-and-password-from-csv-file/#findComment-1099508 Share on other sites More sharing options...
young_coder Posted August 15, 2010 Author Share Posted August 15, 2010 Thank you Floydian.. I will try with help of php.net... Link to comment https://forums.phpfreaks.com/topic/210773-random-username-and-password-from-csv-file/#findComment-1099545 Share on other sites More sharing options...
Psycho Posted August 15, 2010 Share Posted August 15, 2010 Then you can use shuffle() to randomize the order of the array, and reset() to pluck off the first element of that array. Using shufle() is unnecessary and reset() won't do anything productive since the array is already set to the the first element. Once you have the array of values using file(), just use array_rand() to get a random record from the array $randomRecord = $array[array_rand($array)]; Link to comment https://forums.phpfreaks.com/topic/210773-random-username-and-password-from-csv-file/#findComment-1099555 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.