How to count complete sets of items in php? [closed] - php

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 a newbie, so hopefully I word this properly. I have searched and searched both SO and the web to try to find this answer, even mathematically, to understand how to code it, but to no avail.
On a web game, I want to be able to count the number of complete sets a user has (the set has four items).
For example, the user can attain turkey, pumpkin pie, stuffing, and mashed potatoes. Those are displayed in a table by quantity, like so:
Mashed Potatoes: 2
Turkey: 4
Pumpkin Pie: 4
Stuffing: 3
So in this case, the user has 2 total sets of what I'll call a "Thanksgiving Dinner." The Thanksgiving Dinner must include one of each item to be a complete set.
How can I use PHP to count and display the number of complete sets the user has?
This feels like it should be so simple (mathematically, at least), but I am really struggling.

Try this, simple, as shown in this PHP Fiddle
<?php
$min = 100000; // just initialize min with large value.
// these values can be hard coded, from JSON, html form, or database
// it is just written for illustration
$arr = array(
'Mashed Potatoes' => 2,
'Turkey' => 4,
'Pumpkin Pie'=> 4,
'Stuffing'=> 3
);
// we loop through each element of the above array and,
// compare its value to the min.
foreach($arr as $value){
if( $value < $min){
// If the value is less that min. then we store value into min
$min = $value;
}
}
echo 'You can have ' . $min . ' Thanksgiving dinners';
?>
EDIT:
As suggested in a comment from PaparazzoKi, Thanks, you can replace all of this:
foreach($arr as $value){
if( $value < $min){
// If the value is less that min. then we store value into min
$min = $value;
}
}
echo 'You can have ' . $min . ' Thanksgiving dinners';
With this line:
echo 'You can have ' . min($arr) . ' Thanksgiving dinners';
Making use of the .min() function

The total number of complete set will the the key with the lowest value in the array.
You may use the min() function of PHP
http://php.net/manual/en/function.min.php

Related

how to iterate through string and get the second last number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need second last number from the list and the list can have varied length. I tried seperating it using explode(",", $layouts);
$layouts= '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
Example : I need to get 30 from the string here.
please help
If this list can grow in size as you have indicated in the comment, then usually explode it into an array, then do a count of the size, subtract two from the total size and use that as an index to get the value:
//turn this to array
$layouts = '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
$layoutsArray = explode(",", $layouts);
$xtarget = count($layoutsArray) - 2;
$xvalue = "";
if($xtarget > -1){
$xvalue = $layoutsArray[$xtarget];
}else {
//it could be wrong
}
You can explode it with , and get 2nd last value from array try the following code
$layouts= '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
$array = explode(',',$layouts);
if(count($array) > 2){
$pair = array_slice($array, -2, 1, true);
$key = key($pair);
$value = current($pair);
echo $value;
}
Output
30

Trying to get the most of the random element possibily from first 4 items in array php [closed]

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 5 years ago.
Improve this question
Hi am having 10 elements in array . Am trying to get my random element mostly from first 5 elements. Which means random element appearance from first 5 elements should be much greater than next 5 elements
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = $arr[array_rand($arr)];
Am using above one to get the random element normally
Use function rand(min_num, max_num):
function rand5($array) {
$part = rand(1, 10);
return ($part > 3) ? $array[rand(0, 4)] : $array[rand(5, 9)];
}
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = rand5($arr);
Try this simple and easy code :-
$arr = array('a','b','c','d','e','f','g','h','i','j');
$rand = rand(0,9);
echo $arr[($rand <= 6 ? ($rand%5) : $rand)];
You just have to get the random number between 0 to 9 and divide that number in 70%(0-6) and 30%(7-9). If its greater than 5 then only use the remainder else directly get that number
Here is the fiddle :- https://3v4l.org/TWGJJ

Random number every week for all visitors [closed]

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
I want to show a random number between 1,000 and 1,999 with these requirements
Show a new number every Monday
With comma for thousands separator
Show the same number to all visitors
What I found so far:
<?php
srand(floor(date('U')) / (60*60*24*7));
$num = 100;
$a = rand() % $num;
echo $a;
echo "<br>";
$b = rand() % $num;
echo $b;
echo "<br>";
$c = rand() % $num;
echo $c;
echo "<br>";
$d = rand() % $num;
echo $d;
echo "<br>";
$e = rand() % $num;
echo $e;
?>
By creating an appropriate seed you can do this without needing to store the result.
You'll want to get the ISO-8601 week-numbering year and the week number. You can generate this with date('oW'). Today (and until next Monday) that'll return 201638.
The value out of W changes every Monday and the value from o changes every year unless the current day's W belongs to the prior or next year - in which case that year is used. (In other words the year/week combo will never change in the middle of the week due to the new year.)
Once you have that combo use it to seed your random number generator:
mt_srand((int)date('oW'));
Then pull your random number between your limits. With the fixed seed this'll produce the same value for each of your visitors:
$number = mt_rand(1000, 1999);
Then format it to add the thousands separator and output:
echo number_format($number);
All together, skipping the intermediate variable:
mt_srand((int)date('oW'));
echo number_format(mt_rand(1000, 1999));
Today this outputs 1,331.
There's no need to store the generated number anywhere as this reliably regenerates the same number for every visitor, every day until the next Monday - at which point you get a new seed and therefore a new random number (until the following Monday, as so on.)
For more information on the functions used see the PHP manual:
date()
mt_srand()
mt_rand()
number_format()

How to get the numbers after the decimal place for an if statement in php [closed]

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 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.

I need to loop through a list of arrays and return matches [closed]

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 9 years ago.
Improve this question
I am building a game to check lottery tickets so I am trying to build a loop that will loop a 6 lottery numbers through a list of 50 lottery lines.
I have an array with 6 non duplicated numbers. I want to loop this array through 50 arrays each with 6 numbers but in each array no number can be duplicated.
I would like to return how many times the numbers in array match any of the numbers in any of the other 50 arrays.
1 number = 20 matches
2 numbers = 10 matches
3 numbers = 1 match.
I am new enough to PHP and trying to find the easiest way to do this.
I am using this game to improve my knowledge of PHP, any help would be appreciated.
Try something like this:
<?php
//array to store the number of matches
$matchesArr = array(0,0,0,0,0,0,0);
//your lottery numbers
$myNumbers = array(1,2,3,4,5,6);
//the past lottery results
$pastResults = array(
array(10,12,1,2,34,11),
array(10,12,1,2,34,11),
array(10,12,1,2,34,11)
);
//loop through each past lottery result
foreach($pastResult as $pastResult){
$matches = 0;
//do any of your numbers appear in this result?
foreach($myNumbers as $myNumber){
if(in_array($myNumber, $pastResult)){
$matches++;
}
}
//add the number of matches to the array
$matchesArr[$matches]++;
}
//print the number of matches
foreach($matchesArr as $index=>$matches){
echo $index." number = ".$matches."\n";
}
?>

Categories