range - Get a specified rows amount from an array -
i have array 1000 rows , want convert string 50 rows of it.
$thearray = array ( [0] => row1 [1] => row2 [2] => row3 [3] => row4 [4] => row5 [5] => row6 ... [999] => row1000 )
the output should use:
$string1 = implode(',', $thearray);
but said need $string1
have 50 rows array , if possible, them randomized. need advices. thx
you try this, see comments in code explanations , http://3v4l.org/hjuv6 demonstration
// lets create dummy array $array = array(); for($i = 0; $i < 1000; $i++) { $array[] = $i; } // lets make randomized temporary array $backuparray = $array; $temparray = array(); for($i = 0; $i < 50; $i++) { // select random index $randomindex = rand(0 , count($backuparray)); // copy temp array $temparray[] = $backuparray[$randomindex]; // delete row our backup unset($backuparray[$randomindex]); // reorganize key indexes $backuparray = array_values($backuparray); } $string1 = implode(",", $temparray); var_dump($string1);
Comments
Post a Comment