Jump to content

Recommended Posts

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.

 

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

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)];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.