• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

array and radomly select

my program will randomly pick four data from a CSV file.

first data will be first place
second data will be second place
third data will be thirs place
forth data will be forth place

So far, this is the code what I have.

I need to use array_rand(),

How can i implement that in my code?

------------------------------------

$handle = fopen("$file_name", "r");
$data = fgetcsv($handle);
        
while (($data = fgetcsv($handle)) !== FALSE)
{
             
  $record = $data;
     
  foreach ($record as $value)
  {          
     $input = $value;      
           
           /*$rand_keys = array_rand($input, 4);
           echo $input[$rand_keys[0]] . "<br>";
           echo $input[$rand_keys[1]] . "<br>";
           echo $input[$rand_keys[2]] . "<br>";
           echo $input[$rand_keys[3]] . "<br>";
           echo "<br>";  
          */          
      echo $value;
           
  }
}
0
bbvic
Asked:
bbvic
  • 10
  • 8
1 Solution
 
dr_dedoCommented:
do u want to show random 4 records with all their data or random 4 data elemtnts for EACH record ?
0
 
dr_dedoCommented:
<?
$handle = fopen("C:\\ddd.CSV", "r");
while ($data = fgetcsv($handle)){ // reads each line at a time as an array
$newData = array_rand($data,4); // creates a new array with 4 random elements of this record
           echo $newData [0] . "<br>"; //print the 4 random elements
           echo $newData [1] . "<br>";
           echo $newData [2] . "<br>";
           echo $newData [3] . "<br>";
           echo "<br>";
}
?>

is that close to what you have in mind ?
0
 
bbvicAuthor Commented:
well, randomly select 4 data from a csv file is correct, but it gets from each row.

so...based on your code, the result is
a1
a4
a7
a10


b1
b5
b7
b18


---------------------

The result that i can to have is

a1
a5
b5
b9
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bbvicAuthor Commented:
I want to show random 4 records with all their data.
0
 
dr_dedoCommented:
<?
$handle = fopen("$file", "r");

while ($data = fgetcsv($handle)) // reads each line at a time as an array
      $allData[]=$data;
      
           $newData = array_rand($allData,4); // creates a new array with 4 random elements of this record
           echo $newData [0] . "<br>"; //print first element of a random record
           echo $newData [1] . "<br>"; //print second element of a random record
           echo $newData [2] . "<br>"; //print 3rd element of a random record
           echo $newData [3] . "<br>"; //print 4th element of a random record
           echo "<br>";

?>
0
 
bbvicAuthor Commented:
no..got wrong result
0
 
dr_dedoCommented:
<?
$handle = fopen($file, "r");
while ($data = fgetcsv($handle)) $allData[]=$data;
           $newData = array_rand($allData,4);
           foreach ($newData as $n){
            echo $allData [$n][0] . "<br>"; //print first element of a random record
            echo $allData [$n][1] . "<br>"; //print second element of a random record
            echo $allData [$n][2]. "<br>"; //print 3rd element of a random record
            echo $allData [$n][3]. "<br>"; //print 4th element of a random record
            echo "<br>";
           }
?>
0
 
bbvicAuthor Commented:
nope..does not work..

Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array
0
 
dr_dedoCommented:
use
print_r($allData)

see if there is at least for elements in the array (4 records in the CSV file)
0
 
bbvicAuthor Commented:
yes there are.
But what if there are 500 rows, should it be taking more time to select random data?
0
 
dr_dedoCommented:
this error occurs when u ask for 4 random elementes of the array and in the orignal array there is only 1 or 2 or 3 elements.

for sure it would be much slower, because u have to loop through the whole file, that's what db is made for!
0
 
bbvicAuthor Commented:
while (($data = fgetcsv($handle)) !== FALSE)
{
    $allData[] =$data;          
            
}// end of while loop

 
print_r ($allData);

----------------------
the result of this :
Array
(
    [0] => Array
        (
            [0] => a1
            [1] => a2
            [2] => a3
            [3] => a4
       
        )

    [1] => Array
        (
            [0] => b1
            [1] => b2
            [2] => b3
            [3] => b4
           
        )

    [2] => Array
        (
            [0] => c1
            [1] => c2
            [2] => c3
            [3] => c4
         
        )

    [3] => Array
        (
            [0] => d1
            [1] => d2
            [2] => d3
            [3] => d4
           
        )

    [4] => Array
        (
            [0] => e1
            [1] => e2
            [2] => e3
            [3] => e4
             
        )

    [5] => Array
        (
            [0] => f1
            [1] => f2
            [2] => f3
            [3] => f4
             
        )
)

if so..how can i get random 4 data from there?
0
 
dr_dedoCommented:
works fine with me!!
are u sure you have the code this way ?

<?
$handle = fopen("C:\\test1.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE)
{
    $allData[] =$data;          
         
}// end of while loop

           $newData = array_rand($allData,4);
           foreach ($newData as $n){
            echo $allData [$n][0] . "<br>";
            echo $allData [$n][1] . "<br>";
            echo $allData [$n][2]. "<br>";
            echo $allData [$n][3]. "<br>";
            echo "<br>";
           }
?>
0
 
bbvicAuthor Commented:
this is the result that i have now

i1
i2
i3
i4

c1
c2
c3
c4

j1
j2
j3
j4

e1
e2
e3
e4
0
 
bbvicAuthor Commented:
this is data from csv file

---------
A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A ,A
a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20
b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20
c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20
d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20
e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15,e16,e17,e18,e19,e20
f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20
g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17,g18,g19,g20
h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20
i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20
j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20
0
 
bbvicAuthor Commented:
how can i get the number of colum? count of column depends on each file.
0
 
bbvicAuthor Commented:
total number of the column in a file..
0
 
dr_dedoCommented:
glad it finally worked with you
to get the number of columns

<?
$handle = fopen("C:\\test1.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE)
{
    $allData[] =$data;          
 $num= sizeof($data);        
}// end of while loop

           echo "<br>num=$num<br>";
           $newData = array_rand($allData,4);
           foreach ($newData as $n){
            echo $allData [$n][0] . "<br>";
            echo $allData [$n][1] . "<br>";
            echo $allData [$n][2]. "<br>";
            echo $allData [$n][3]. "<br>";
            echo "<br>";
           }
?>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now