My randomize function does not work properly.
I have an array which is getting randomized.
The array contains three values but only two are chosen each time.
$sterren = array("3","4","5");
$sterrenr = array_rand($sterren, 1);
$sterrenf = $sterren[$sterrenr[0]];
echo $sterrenf;
During the loop when outputting echo $sterrenf only the values 3 and 4 appear but no value 5.
Anyone any ideas ?
When the optional $num parameter is set, array_rand() returns $num random keys. In this case, you're setting the second parameter 1, so you'll get a single key. You just need to echo the corresponding array element:
Change:
$sterrenf = $sterren[$sterrenr[0]];
to:
$sterrenf = $sterren[$sterrenr];
Very easy look with:
First to retrieve the three elements point array_rand to (~,3) not to (~,1).
Second to random as you think is by shuffle in php not by array_rand because array_rand will return a random selection but will rearrange them inside it so after it use "shuffle()" on the output of array_rand "result array" like this what i mean:
$sterren = array("3","4","5");
$sterrenr = array_rand($sterren, 3);
shuffle($sterrenr);
$sterrenf = $sterren[$sterrenr[0]];
echo $sterrenf;
Related
I have two values: #FFF & #000
I want to echo randomly anyone, for that I tried:
$color = array('#FFF','#000');
$color = array_rand($color);
$color = $color[$color];
echo $color;
But it doesn't display anything.
You must need to take care of 2 things,
One, if you overwrite the variable with same name then, it will not produce your desired result.
Second, array_rand() will generate the random key
Why your code is not working, because, your initial array is $color:
$color = array('#FFF','#000');
After this, you are using array_rand(), which will give random key
$color = array_rand($color);
Now, your $color is overwrite from array to integer value. it means when you try to access $color[$color] this will give you nothing because, its not defined or overwritten.
Solution: just change your variable name specially array. using proper variable name is a good approach and this will help to others who will work on your work after you.
Example:
<?php
$colorArray = array('#FFF','#000');
$colorKey = array_rand($colorArray);
$colorName = $colorArray[$colorKey];
echo $colorName;
?>
Capture random index in other variable let say $key.
$color = array('#FFF','#000');
$key = array_rand($color);
echo $color[$key];
Note: array_rand — Pick one or more random keys out of an array
Demo
You had a good idea using array_rand
array_rand — Pick one or more random keys out of an array
However you are overwriting your array. ($color)
Try that instead :
$arrayRand = array_rand($color);
$randomColor= $color[$arrayRand];
i want to display in my system just one record with the same id.
for example: 01,01,02,02,03,03.
I just want to display 01,02,03.
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
$number = "01,01,02,02,03,03";
$result = implode(',',array_unique(explode(',', $number )));
echo $result; // output 01,02,03
Happy Programming
I got a variable of ingredient name and ingredient value
I wanted to know if they have equal number of items. I used implode.
for ex.ingredient_name['chicken'^'meat'^'water'] = 3 .
$ingredient_name =implode("^",$_POST["mytext"]);
$ingredient_value = implode("^", $_POST["mytext2"]);
implode takes array as a parameter so your code should work fine with
implode("^",$_POST["mytext"]);
implode("^",$_POST["mytext2"]);
so
if(count($_POST["mytext"])===($_POST["mytext2"])) echo "equal";
should do the job
I am trying to re-create an array by choosing random elements of an existing array using array_rand().
However, when using the following code and spitting out the array, I am getting an undefined index error in reference to it.
The array is $city_users_trimmed.
PHP code:
$rand = array_rand($city_users_trimmed, 1);
$data = array($city_users_trimmed[$rand[0]]);
I know that $city_users_trimmed has at least 1 element in it, so there is no reason I see why this wouldn't be working.
I tested it by running:
$data = array($city_users_trimmed[0]);
which works, so there is at least one element in the $city_users_trimmed array. Is there anything you see that is causing this issue? Thank you.
Check http://php.net/manual/en/function.array-rand.php
When picking only one entry, array_rand() returns the key for a random entry. Otherwise, an array of keys for the random entries is
returned.
So in $rand is your key, not in $rand[0]
According to the doc
mixed array_rand ( array $array [, int $num = 1 ] ) Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
as you asked 1 key, $rand is an int
$city_users_trimmed = array(1,2,3,4,5,6,6,7,8,8,9,90,8,6,5,4,5,76,78,75,643,54,543,53,43,5345,34534,535);
var_dump($city_users_trimmed);
$rand = array_rand($city_users_trimmed, 1);
var_dump($rand);
$data = array($city_users_trimmed[$rand[0]]);
var_dump($data);
this code will give you the Notice Undefined index in line 5 because of "$data = array($city_users_trimmed[$rand[0]]);"
you cant treat with an simple variable as an array.
so right code will be
$city_users_trimmed = array(1,2,3,4,5,6,6,7,8,8,9,90,8,6,5,4,5,76,78,75,643,54,543,53,43,5345,34534,535);
var_dump($city_users_trimmed);
$rand = array_rand($city_users_trimmed, 1);
var_dump($rand);
$data = array($city_users_trimmed[$rand]);
var_dump($data);
If I have:
$mainarray = some array of things with some repeated values
$array_counted = array_count_values ($mainarray);
How can I find the maximum value in $array_counted?
(This would be the element that appeared most often in $mainarray I think. Its mostly a syntax issue as I am pretty sure I could loop it, but not sure of the syntax to use)
You can find first max value as
$main_array = array(1,2,3,4,5,6,6,6,6,6,6,6);
$max_val = max($main_array);
for find all of max vals
in php < 5.3
function findmax($val)
{
global $max_val;
return $val == $max_val;
}
$max_values_array = array_filter($main_array,'findmax');
in php >= 5.3
$max_values_array = array_filter($main_array,function($val) use ($max_val) { return $val == $max_val; });
echo count($max_values_array);
var_dump($max_values_array);
You could sort the array and take the first, respectively last item out of it, if you don't want to loop.
Since you associate the count with the values with that:
$array_counted = array_count_values ($mainarray);
You only need to sort it afterwards, and return the first key (which is the most occured element):
arsort($array_counted);
print key($array_counted); // returns first key
ok, the guy whos answer I used has deleted his comment so here is how I did it:
I used arsort($array_counted) to sort the array, while keeping index. rsort alone does not work as the result of array_count_values is an associative array. Thank you all.