Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For example: A = 2, B = 4 & C = 2 then output should be uniqueNumber(A, B, C) = 4
if ($A==$B)
{
return $C;
}
if ($A==$C)
{
return $B;
}
return $A;
This should be a simple logic answer and you can either go with direct comparisons but that might be too hectic in case of more than 3 arguments.
You can try this out as well:
$num_arr = [$a,$b,$c];
for($i=0;$i<count($num_arr)-1; $i++)
{
if(!in_array($num_arr[$i],array_merge(array_slice($num_arr,0,$i),array_slice($num_arr,$i+1))))
return $num_arr[$i];
}
Or simply like this:
$num_arr = [$a,$b,$c];
for($i=0;$i<count($num_arr)-1; $i++)
{
$temp = $num_arr;
unset($temp[$i]);
if(!in_array($num_arr[$i],$temp))
return $num_arr[$i];
}
Tried implementing a more generic approach.
Hope it helps.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want split one group AS follow
splitGroups("11133355557777") ➞ ["111", "333", "5555", "7777"]
Anyone have idea then let me know
With php version 5 above this will work link to execute
Link to an example https://paiza.io/projects/qfRZ07OP3OviWCVsUbdFtQ
function splitGroups($str){
$arr = [];
$i=0;
$sub = '';
while($i!=strlen($str))
{
$sub .= $str[$i];
if ( strlen($str)-1 == $i || $str[$i] != $str[$i+1] ){
$arr[] = $sub;
$sub='';
}
$i++;
}
return $arr;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to replace 200 numbers with an image?
This example :
For example
When user have 200 numbers He takes 1 image
When user have 830 He take 4 image
what php code I need it?
Sorry but I havn't Any code
Thanks in advance
$votes = 10334;
$starCount = intval($votes/ 200);
$starCount = $starCount > 5 ? 5 : $starCount; //if maximum 5 stars
$a = 1;
$starsString = '';
for ($a; $a <= $starCount; $a++) {
$starsString .= '⛤'; // or '<img src="https://i.stack.imgur.com/EhAy4.gif" alt="here">'
}
echo $starsString;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If i have a loop for example
for ($a=1; $a<6; $a++)
Is it possible to say for the first loop if another variable like $b is the same as one of the other numbers that will come like 2, 3, 4 or 5.
And what if i have a word split, so every letter is a variable like this:
$letter1 = str_split($word)[0];
$letter2 = str_split($word)[1];
$letter3 = str_split($word)[2];
$letter4 = str_split($word)[3];
$letter5 = str_split($word)[4];
How can i check if $a is at 1 in the loop,if it is not letter1 but letter2, 3, 4 or 5?
sure. Just check it against the counter:
for ($a=1; $a<6; $a++){
if ($a===$b){
echo ("b is caught!");
break;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have long decimal numbers that I'd like to make easier to read. There's many 0's involved and I would like to bold the end of the string from where the numbers stop being just 0's. The number will always have 8 decimal places, no more, no less. It's worth noting that I would like any trailing zeros to also be bold, I don't just want to avoid bolding 0 as a whole.
Example:
Before - 0.00004320
After - 0.00004320
Any help or pointers in the right direction much appreciated.
Thanks.
You can do it using a loop and some string manipulation:
function doTheThing($number) {
$length = strlen($number);
$period = strpos($number, '.') + 1;
for ($i = $period; $i < $length; $i++) {
if ($number[$i] !== '0') {
$number = substr($number, 0, $i).'<b>'.substr($number, $i).'</b>';
break;
}
}
return $number;
}
echo doTheThing('0.00004320');
Or you can do it using a regular expression replacement:
function doTheThing($number) {
return preg_replace('/^(\d+\.0*)(\d+)$/', '$1<b>$2</b>', $number);
}
echo doTheThing('0.00004320');
echo doTheThing(sprintf('%.8f', 0.00004320)); // if your number is not a string