I want to insert a huge list of contacts in my database (for some performance tests).
I have some troubles with randomizing some data.
$input = array("city1", "city2", "city3");
$city= shuffle($input);
If I understood clear the shuffle
function, I need to pass it an array and will just suffle it, right? Then what is wrong here?
PS. I know, just basic PHP stuffs, but I couldn't find any similar example with arrays here on stackoverflow, so please don't get angry for opening this topic.
Your bit of code: $city=shuffle($input);
doesn't pass the shuffled array to the $city
variable - but rather the function return which is TRUE or FALSE depending on whether it completed successfully. The function actually shuffles your ORIGINAL array.
You are assigning the Boolean (true or false) to your $city array - which is the output of the function - ie, did it work.
<?php
$input = array("city1", "city2", "city3");
print_r($input);
shuffle($input);
print_r($input);
?>
The Shuffle function shuffles the original array.
Output:
Array
(
[0] => city1
[1] => city2
[2] => city3
)
Array
(
[0] => city1
[1] => city3
[2] => city2
)
Edit: Answering comment:
You access the individual elements of the array using the standard PHP format of square brackets and a key?
<?php
$input = array("city1", "city2", "city3");
// Access one element of the array:
echo $input[0];
// shuffle the array about.
shuffle($input);
// Access the same element of the array
// and see if the data is the same
echo $input[1];
?>
The output of this is a string with any of the bits of the array, then the same element - which may or may not have the same output.