I need to select two words without repeat them (PHP) - php

I have a array list and I want to select two words from this list without repeat they. But I tried many stuff and nothing worked. There is the current code:
$randomq2[] = "one";
$randomq2[] = "two";
$randomq2[] = "three";
srand ((double) microtime() * 1000000);
$getrando1 = rand(0,count($randomq2)-1);
$getrando2 = rand(0,count($randomq2)-1);
$wordone = $getrando1[$conco1];
$wordtwo = $getrando2[$conco2];

This should work for you:
(Here I first get all unique elements from $randomq2 with array_unique(). Then I simply shuffle() the array and at the end I just extract 2 elements from the start with array_slice())
<?php
$randomq2 = array_unique($randomq2);
shuffle($randomq2);
$random = array_slice($randomq2, 0, 2);
print_r($random);
?>

you can go following way:
$getrando1 = rand(0,count($randomq2)-1);
$getrando2 = rand(0,count($randomq2)-1);
while ($getrando2 == $getrando1)
$getrando2 = rand(0,count($randomq2)-1);
also - I'd suggest you to use mt_rand instead of simple rand

I didn't understand your question very well
but try this
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
this will remove duplicated values in the array

Related

PHP Using rand to pick between specific numbers

I know that rand(1,10); would generate a random number between 1 and 30.
How would I go about writing code that would pick a random number out of a group of numbers say 1,7,8 and 9?
Is it possible?
I am pretty sure rand is set up to only generate numbers within a range?
First thing is that rand(1,10) will generate a number between1 to 10 not 1 to 30.
PHP RAND FUNCTION
From a group of numbers, you can generate like this
$numbers = [1,7,8,9];
echo $numbers[rand(0,3)]
You can create custom array which you required, shuffle and get first value
$temp = [1,7,8,9];
shuffle($temp);
echo $temp[0];
shuffle — Shuffle an array
Demo
You could put your group of numbers into an array, and then use array_rand or shuffle to select one of them. For example:
$nums = array(1, 7, 8, 9);
$key = array_rand($nums);
echo $nums[$key] . PHP_EOL;
shuffle($nums);
echo $nums[0] . PHP_EOL;
Demo on 3v4l.org
Use this function (or inline code)
function SelectElementByRandom ( $list )
{
return count($list) ? $list[ rand(0,count($list)-1) ] : NULL;
}
The ?: operator ensures an result (NULL), even if the list is empty.
And for your example:
$result = SelectElementByRandom(array(1,7,8,9));
I would approach it like this. Create a loop that fills an array with random numbers in the range you're looking for (your group of numbers). After that, you choose a random index in that array, which is a random number in your group of numbers.
$min = 0;
$max = 10;
$group_size = 5;
$rand_group = array();
for($i = 0; $i < $group_size; $i++) {
$rand_group[$i] = rand($min,$max);
}
echo $rand_group[rand($min,$max)];

Parts of variables save to another var

I have this autogenerated code:
$code = "k9sdhfkr9235kdh5|fdh4hnchjgrj";
How can I save to a var the first and the last part of this code like this?
$first = "k9sdhfkr9235kdh5";
$last = "fdh4hwshnchjgrj";
The code is always separated by this character (|) and the code consists random character number so sometimes it is 16 characters, sometimes 11 etc...
foreach($code as $v){
$pos = strpos($v, "|");
$first = substr($v,...?
You may use the explode
explode("|",$code );
It will return an array of values
You can use builtin functionality called explode(); given below the solution for your question
<?
$code = "k9sdhfkr9235kdh5|fdh4hnchjgrj";
$codeArr=explode("|",code);
$first = $codeArr[0];
$last = $codeArr[1];
?>
Please try this. More about the explode function is here

Php variable random definite value

I try to create a variable who send a random value !
$random = mt_rand(5,10);
but i want the variable random with the number i choose like this:
$random = 5;10;15;20;25;30
The variable must send randomly 5 or 10 or 15 or 25 or 30 only
What you could do is using the mt_rand function with values from 1 to 6 then multiply them by 5.
Example:
$random = mt_rand(1, 6) * 5;
Like that?
$random = mt_rand(1,6) * 5;
Use array_rand to pick random key from array then you can put it into an another.
http://php.net/manual/en/function.array-rand.php
$input = array(5,10,15,20,25);
$rand_key = array_rand($input);
$another_array[] = $input[$rand_key];
You could use and array of element and get a random item
$items = Array(5,10,15,20,...50);
$myvalue = array_rand($items);
Assuming that you already have the values you want inside an array you can simply get a random position of the array. A rand() limited by the first element to the size of the array can solve this
<?php
$valores = [1,5,12,17,22,30,90,2,4,6]; // choose de values here
for($i=0;$i<200;$i++){ // just to print more than 1 value
echo $valores[rand(1,sizeof($valores))-1]."<br>"; // really pick the values here. start in 1 because we'll remove 1
}
Will it always be steps of 5? Then you could do something like
$random = mt_rand(1,6) * 5;
Otherwise I'd put the numbers that you want to choose from in an array and select a random element using mt_rand():
$random_temp = array(3,6,8,13,16,19);
$random = $random_temp[mt_rand(0,6)];
If the random numbers arrayed, then this may help:
$list=array(5,10,15,20,25,30);
$n=count($list)-1;
$random=$list(rand(0,$n));
$values = [5,10,15,20,25,30];
//shuffle the array
shuffle($values);
foreach($values as $v){
print $v;
}
always in different order...
You can use rand():
$random = rand(1, 6) * 5;
Will produce:
5, 10, 15, 25 or 30 randomly.
Please try this code
$random = rand(0,30);
if(($random%5)==0)
{
echo $random;
}
else
{
echo roundUpToAny($random);
}
function roundUpToAny($n,$x=5) {
return round(($n+$x/2)/$x)*$x;
}

How to get last iteration of for loop when limit and increment is dynamic?

I have seen some question related to this one like
How to check for last loop when using for loop in php?
Last iteration of enhanced for loop in java
but I am not getting exact solution because in my case increment and end limit both are dynamic.
Requirement:- I do not want comma after last element printed.
$inc=11;
$end=100;
for($i=1;$i<=$end;$i=$i+$inc){
echo $i==$end?$i:"$i,"; // 1,12,23,34,45,56,67,78,89,100
}
In above code I know that last element($i) will be 100($end).
So I can write condition as $i==$end but in below case it won't work.
$inc=12; // Now $inc is 12
$end=100;
for($i=1;$i<=$end;$i=$i+$inc){
echo $i==$end?$i:"$i,"; // 1,13,25,37,49,61,73,85,97,
}
Now last element 97 has comma which I need to remove.
Thanks in advance.
Keep it simple:
$numbers = range(0, $end, $inc);
$string = implode(",", $numbers);
echo $string;
You can see it here: https://3v4l.org/BRpnH
;)
You can just use,
echo $i+$inc>$end?$i:"$i,";
It checks whether this is the last possible iteration instead.
Use rtrim to remove the last comma
$inc = 12; // Now $inc is 12
$end = 100;
$print = '';
for($i=1;$i<=$end;$i=$i+$inc){
$print .= ($i==$end)? $i : "$i,"; // 1,13,25,37,49,61,73,85,97,
}
$print = rtrim($print, ',');
echo $print;
You can do it in this way :
<?php
$inc=4;
$end=10;
for($i=1;$i<=$end;$i=$i+$inc){
echo ($i+$inc-1)>=$end?$i:"$i,"; // 1,5,9
}
?>
This code is working on prime number case also not given you result like // 1,13,25,37,49,61,73,85,97, always gives you result like // 1,13,25,37,49,61,73,85,97 no comma added after any prime number.

Isolate substring at end of string after a specific substring

My query generates a result set of UID values which looks like:
855FM21
855FM22
etc
I want to isolate the last number from the UID which it can be done by splitting the string.
How to split this string after the substring "FM"?
To split this string after the sub string "FM", use explode with delimiter as FM. Do like
$uid = "855FM22";
$split = explode("FM",$uid);
var_dump($split[1]);
You can use the explode() method.
<?php
$UID = "855FM21";
$stringParts = explode("FM", $UID);
$firstPart = $stringParts[0]; // 855
$secondPart = $stringParts[1]; // 21
?>
use explode function it returns array. to get the last index use echo $array[count($array) - 1];
<?php
$str = "123FM23";
$array = explode("FM",$str);
echo $array[count($array) - 1];
?>
For it,please use the explode function of php.
$UID = "855FM21";
$splitToArray = explode("FM",$UID);
print_r($splitToArray[1]);
Have you tried the explode function of php?
http://php.net/manual/en/function.explode.php
As a matter of best practice, never ask for more from your mysql query than you actually intend to use. The act of splitting the uid can be done in the query itself -- and that's where I'd probably do it.
SELECT SUBSTRING_INDEX(uid, 'FM', -1) AS last_number FROM `your_tablename`
If you need to explode, then be practice would indicate that the third parameter of explode() should set to 2. This way, the function doesn't waste any extra effort looking for more occurrences of FM.
echo explode('FM', $uid, 2)[1]; // 21
If you want to use php to isolate the trailing number in the uid, but don't want explode() for some reason, here are some wackier / less efficient techniques:
$uid = '855FM21';
echo strtok($uid, 'FM') ? strtok('FM') : ''; // 21
echo preg_replace('~.*FM~', '', $uid); // 21
echo ltrim(ltrim($uid, '0..9'), 'MF'); // 21
$uid = '123FM456';
$ArrUid = split( $uid, 'FM' );
if( count( $ArrUid ) > 1 ){
//is_numeric check ?!
$lastNumbers = $ArrUid[1];
}
else{
//no more numbers after FM
}
You can also use regular expressions to extract the last numbers!
a simple example
$uid = '1234FM56';
preg_match( '/[0-9]+fm([0-9]+)/i', $uid, $arr );
print_r($arr); //the number is on index 1 in $arr -> $arr[1]

Categories