I have an array and I am getting its length using sizeof(). In the size of the array I want to get how many 10 there are in that number. I used % in getting my number like:
$arraysize = 13;//sizeof($array)
$numberoften = $arraysize % 10 // getting 3
From here I know that I am wrong. What i want to get is 2. Because I want to get every 10. Like if length is 9 I want to get 1. If length is 31 I want to get 4.
What is the correct function of way to get my desired output?
What you're after is ceil()
Returns the next highest integer value by rounding up value if necessary.
$numberoften = ceil($arraysize / 10);
There's also floor() if you want to round the number down to the nearest integer. This would seem to fit better with your question...
I want to get how many 10 there are in that number
Use ceil() function
$arraysize = 13;//sizeof($array);
$numberoften =ceil( $arraysize/10);
Or
Use round() function
$arraysize = 13;//sizeof($array);
$numberoften =round( $arraysize/10);
Related
I have a PHP problem.
I need to write a number from a sets of digits 0-9. Each set has 10 digits, each digit once.
I need to count the number of sets that I have to use to write the number.
For example number 10 is written from one set, but number 300 uses 2 sets because it has two zeros.
But, the problem is that 6 and 9 are considered the same. They can be rotated by 180 degrees.
Number 266 will be using one set, 369 also is using one set, but 5666 is using 2 sets.
I would be very grateful if you could somehow help me.
Here is how I have started and stuck up, have no more clue how to loop through it. Tried many things, nothing successful.
<?php
function countSet($num) {
$array = str_split($num);
$statarr = [0,1,2,3,4,5,6,7,8,9];
$a1 = $array; $a2 = $statarr;
$result = array_intersect($a1,$a2);
$count = array_count_values($result); }
?>
If you just need to know how many sets you need for a number, you can solve by counting the numbers. For the case of the 9, just replace every 9 by a 6 and divide the number of 6 by two. Something like this (sorry if there's any syntax error, I'm on mobile):
function countSet($input) {
// Convert the input into a string and replace every 9 by a 6. Then convert it to array
$numbers = str_split(str_replace("9", "6", strval($input)));
// Count occurrences for each number
$usedNumbers = array_count_values($numbers);
// If we have a 6, must divide it by 2 (6 represents 6 and 9
if (array_key_exists("6", $usedNumbers)) {
$usedNumbers["6"] = ceil($usedNumbers["6"] / 2);
}
// Now we just need to know the max number of occurrences for a single number as our result.
return max($usedNumbers);
}
See an online demo here
I'm looking to create a PHP Range loop
In a first and second number in the range, but have noticed the first number which is
00006
Is being rounded down / flattened to show "6".
So when I echo the first number value I get "6" back. I need it to be "00006"
Then the next number will need to be 00007 and so on, via the range loop.
My PHP code at present is :
$first_number = 00006;
$last_number = 11807;
foreach(range($first_number, $last_number) as $id)
{
echo $id;
}
How do I go about making sure that the number has the previous 0's in it?
You can do it by using printf() function.
See the documentation : printf
First of all in PHP, a number starting with zero is treated as octal number, But I guess range() function converts it as decimal. So if you want to start it with 20. Like $first_number = 00020; then the output will be start with 16 not 20.
So, if you want the output starting with 0's, then you can do like this:
$first_number = 6;
$last_number = 11807;
foreach(range($first_number, $last_number) as $id)
{
printf("%05d",$id);
}
This might seem like a very simple thing, and maybe I'm just not understanding the question? Anyway. $a and $b are random numbers from 1000 to 10000. I need to take the largest one and round it to the nearest hundred.
Is this supposed to be like so:
$a = 5;
$b = 10;
echo mt_rand($a,$b);
But then, how do I get the largest number and round it?
I don't know why this confuses me so much.
Also, apparently, I am supposed to use max somewhere.
You need to add the values to an array then use max of the array.
To round the value to closest 100 you need to use the following formula round(x/100)*100
This example makes 10 random numbers and echo the max value and the rounded max value.
For($i=0;$i<10;$i++){
$a[] = mt_rand(1000,10000);
}
//Var_dump($a);
Echo max($a). "\n"; // max number
Echo round(max($a)/100)*100; //max number rounded
https://3v4l.org/NRWiH
Example:
I have an array with 3 values:
0 = 1
1 = 4
2 = 5
I want to get a random number like
$random = rand(1, 5);
But I need to get a number that is different from the array values. I need it to return 2 or 3.
This should work for you:
(Here I create the range from where you get your random number with range(). Then I get rid of these numbers which you don't want with array_diff(). And at the end you can simply use array_rand() to get a random key/number)
<?php
$blacklist = [1, 4, 5];
$range = range(1, 5);
$randomArray = array_diff($range, $blacklist);
echo $randomArray[array_rand($randomArray, 1)];
?>
output:
2 or 3
EDIT:
Just did some benchmarks and the method with the loop is much slower than the code above!
I created an array(blacklist) from 1...100'000 and a random number array from 1... 100'001.
So that script should only create one/unique random number. With the loop method you get an error:
Fatal error: Maximum execution time of 30 seconds exceeded
And with the posted code above it takes 1.5 sec in average.
What would be a good way to generate 7 unique random numbers between 1 and 10.
I can't have any duplicates.
I could write a chunk of PHP to do this (using rand() and pushing used numbers onto an array) but there must be a quick way to do it.
any advice would be great.
Create an array from 1 to 10 (range).
Put it in random order
(shuffle).
Select 7 items from the array (array_slice)
Populate an array with ten elements (the numbers one through ten), shuffle the array, and remove the first (or last) three elements.
Simple one-liner:
print_r(array_rand(array_fill(1, 10, true), 7));
Check out the comments in the php manual, there are several solutions for this.
An easy one is this one:
$min = 1;
$max = 10;
$total = 7;
$rand = array();
while (count($rand) < $total ) {
$r = mt_rand($min,$max);
if (!in_array($r,$rand)) $rand[] = $r;
}
Whole numbers? Well, if you want 7 out of 10 then you more efficiently DON'T want 3 out of 10.
Feel free to use any of the other responses but instead of creating 7 numbers start with 10 and eliminate 3. That will tend to speed things up by more than double.
The "shuffle" method has a MAJOR FALW. When the numbers are big, shuffle 3 billion indexs will instantly CAUSE 500 error. Here comes a best solution for really big numbers.
function getRandomNumbers($min, $max, $total) {
$temp_arr = array();
while(sizeof($temp_arr) < $total) $temp_arr[rand($min, $max)] = true;
return $temp_arr;
}
Say I want to get 10 unique random numbers from 1 billion to 4 billion.
$random_numbers = getRandomNumbers(1000000000,4000000000,10);
PS: Execution time: 0.027 microseconds