How to make sequential number and repeat it - php

I would like to make a number sequential and repetitive to the beginning when it reaches the limit. For instance, the first number begins with 001, then 002, 003, and so on until it reaches 999. When it reaches 999, then the number will return to 01 and continues to repeat.
I currently have this loop:
$i = 001;
for ( $i; $i < 111; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
}
But how can I make that return to the first number and continues like before? I mean if the number reaches 999, it will back to 001, 002, 003, and so on.

Even if I don't quite understand why you want to do that, this is your solution :
<?php
for ($i = 001, $max = 111; $i < $max; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
if ($i == $max - 1) {
$i = 001;
}
}
But this will cause an infinite loop of course.

Related

How can I count the max amount of numbers, which are increasing in a row by date in PHP

My goal is to get the count of the max amount of numbers, which are increasing in a row in a certain date range. Below is an array. The correct answer of the max amount increasing numbers between 01/13/2021 and 02/17/2021 would be 5 as the numbers 3,8,10,13,15 are growing in that date range.
I managed to search in a certain period of time...
$row = array(
array('01/02/2021', 1),
array('01/13/2021', 4),
array('01/15/2021', 6),
array('01/19/2021', 9),
array('01/30/2021', 5),
array('02/03/2021', 4),
array('02/11/2021', 3),
array('02/12/2021', 8),
array('02/15/2021', 10),
array('02/16/2021', 13),
array('02/17/2021', 15),
array('02/18/2021', 16)
);
$startDateNew = date('m/d/Y', strtotime("2021-01-13"));
$endDateNew = date('m/d/Y', strtotime("2021-02-17"));
foreach($row as $x) {
if(($x[0]>=$startDateNew)&&($x[0]<=$endDateNew) ){
echo 'Found ';
print_r($x);
}
}
I managed also to search the first increasing number sequence in a one-dimensional array as the answer of the code below is 4. But I didn't succeed to continue this search after if-sentence to the next increasing sequence(and the next and the next), so that the correct answer would be 5 in this case.
Including to that, these two searches should combine somehow together.
$numbers = array(1,4,6,9,5,4,3,8,10,13,15);
function LCIS($numbers) {
$counter = 1;
$answer = 1;
for($i = 0; $i < count($numbers) -1; $i++) { //
if ($numbers[$i] < $numbers[$i+1]) { //comparing array indexes together
$counter++; //
$answer = max($answer, $counter); //this doesn't do anything spectacular right now...
continue;
}else {
$counter == 1;
}
return $answer;
}
}
echo LCIS($numbers);
I'm very beginner at php coding. Could you help me, please?
Well if you have already 2 parts of the code you just need to combine them together. What I suggest is to feed the $numbers array during checking if the $row array element is in certain date range.
function LCIS($numbers)
{
$counter = 1;
$answer = 1;
for ($i = 0; $i < count($numbers) - 1; $i++) { //
if ($numbers[$i] < $numbers[$i + 1]) { //comparing array indexes together
$counter++; //
$answer = max($answer, $counter); //this doesn't do anything spectacular right now...
} else {
$counter = 1;
}
}
return $answer;
}
$row = array(
array('01/02/2021', 1),
array('01/13/2021', 4),
array('01/15/2021', 6),
array('01/19/2021', 9),
array('01/30/2021', 5),
array('02/03/2021', 4),
array('02/11/2021', 3),
array('02/12/2021', 8),
array('02/15/2021', 10),
array('02/16/2021', 13),
array('02/17/2021', 15),
array('02/18/2021', 16)
);
$numbers = array();
$startDateNew = date('m/d/Y', strtotime("2021-01-13"));
$endDateNew = date('m/d/Y', strtotime("2021-02-17"));
foreach($row as $x) {
if(($x[0]>=$startDateNew)&&($x[0]<=$endDateNew) ){
$numbers[] = $x[1];
}
}
echo LCIS($numbers);
BTW: I have modified a little your LCIS function and it will run faster and more correct.

Getting nth-child with an offset in PHP with modulus

When iterating through a loop, I've used the modulus operator in an if statement to obtain nth results pretty easily like this:
// Get 5th item in series
if ($variable->array_item % 5 == 0) {
echo $variable->array_item;
}
How do you do get the 5th item in a series with an offset of 3 (ie, 3,8,13,18,23,etc.)?
I've seen a couple of methods but I'm looking for a canonical answer and I really don't see one on S.O. right now.
Your code specifically requests numbers that are evenly divisible by 5, whereas what you want are numbers 3, 8, 13, 18, 23, etc. This is easy enough to do using almost identical code to what you have:
// set up some test data
$testArray = [];
for ($i = 1; $i <= 30; ++$i) {
$testArray[$i] = "Testing {$i}";
}
// here's where the actual work happens
foreach ($testArray as $key => $value) {
if ($key % 5 == 3) { // <-- note 3, not 0
echo "Found $value\n";
}
}
$iterator = 0;
$step = 3;
while($iterator < count($collection))
{
echo $collection[$iterator]
$iterator += $step
}

php count even numbers in an array

So I am trying to learn php, and the code for finding even numbers doesn't output anything, but I can't seem to find the error, can anybody find where I have made my stupid mistake? Here is the code:
<?php
/*this sets the array up with the data*/
$myarray = array(1,2,3,4,5,6,7,8,9,10);
/* this is the count to get the total number from my array */
$total = count($myarray);
?>
<h1>Display all even numbers</h1>
<ul>
<?php for ($i=1; $i < total; $i += 2): ?>
<li>The array element value is <?php echo $myarray[$i]; ?>. </li>
<?php endfor; ?>
</ul>
Thanks and if nobody wants to post an answer I understand new questions are frustrating.
thanks
Your code isn't finding even numbers. You're identifying their positions in the array, and printing values only for those indexes. Look at this php snippet.
<?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
// Array indexes start at 0, not 1.
for ($i = 0; $i < count($myarray); $i++) {
echo "Index ", $i, ", value ", $myarray[$i], ": ";
// A value is even if there's no remainder when you divide it by 2.
if ($myarray[$i] % 2 == 0) {
echo "even\n";
}
else {
echo "odd\n";
}
}
?>
Put that in a file, and run it through php. You should see this.
Index 0, value 1: odd
Index 1, value 2: even
Index 2, value 3: odd
Index 3, value 4: even
Index 4, value 5: odd
Index 5, value 6: even
Index 6, value 7: odd
Index 7, value 8: even
Index 8, value 9: odd
Index 9, value 10: even
This shorter version will print only the even values.
<?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
for ($i=0; $i < count($myarray); $i++) {
if ($myarray[$i] % 2 == 0) {
echo "Index ", $i, ", value ", $myarray[$i], "\n";
}
}
?>
Index 1, value 2
Index 3, value 4
Index 5, value 6
Index 7, value 8
Index 9, value 10
You're missing the $ variable creator in your for loop for total:
<?php for ($i=1; $i < $total; $i += 2): ?>
This code fits your problem.
<?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
$total = count($myarray);
echo "<h1>Display all even numbers</h1>";
echo "<ul>";
foreach($myarray as $rw)
{
if(($rw%2) == 0 ){
echo "<li>".$rw."</li>";
}
}
echo "</ul>";
?>
The modulus operator % is the best way to get the odd or even numbers in an array.
printing a even number in php. there $ais 20 when you run this code the out put is 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
$a = 20
for ($i=0; $i < $a; $i += 2)
{
echo "</n><br>".$i;
}
Out put like this
2
4
6
8
10
12
14
16
18
20

PHP make combination with exponents

I've been looking in to a math problem: to find the factors of a large number.
I've come to the method of "prime factorization", that all went well to code in php.
But then, say i want to know the factors of the number 196 (being: 1, 2, 4, 7, 14, 28, 49, 98, 196), i've found that the prime factorization of this number is: (2^2)(7^2).
To find the factors, you'll have to make all the possible combinations between the two and muttiply them:
(2^0)(7^0) = 1
(2^1)(7^0) = 2
(2^2)(7^0) = 4
(2^0)(7^1) = 7
(2^1)(7^1) = 14
(2^2)(7^1) = 28
(2^0)(7^2) = 49
(2^1)(7^2) = 98
(2^2)(7^2) = 196
This is where i'm stuck.
I need to find a function that will make a combination of these items (the exponent may not be higher then the one is the prime factorization of that number). This function will have to work on N factors (N is a number > 0 and smaller than 100).
I hope you understand my problem and have some ideas on how to solve it!
The following will print the prime factor of $N = '196' along with the number of exponents.
<?php
function is_prime($N) {
if($N == 2) return true;
for($i = 3 ; $i * $i <= $N; $i++ ) {
if($N % $i == 0) return false;
}
return true;
}
$a = array();
$N = '196';
for($i = 2; $i <= $N; $i++) {
while($N % $i == 0 && is_prime($i)) {
if(!isset($a[$i])) {
$a[$i] = 0;
}
$a[$i]++;
$N = $N / $i;
}
}
print_r($a);
?>

how to iterate through every x numbers and echo results in php?

the question title might be a bit confuse. but here is my case.
i would like my result to like something like:
0000000000
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
0000000010
0000000001
0000000011
0000000012
0000000013
0000000014
0000000015
0000000016
0000000017
0000000018
0000000019
0000000002
0000000020
0000000021
0000000022
0000000023
...
0000000003
...
0000000004
...
i can get the first numbers by doing this:
for ($i = 1; $i<=10; $i++){
$nr=str_pad($i, 10, "0", STR_PAD_LEFT);
echo $nr.'<br>';
}
but then how do i get the numbers in between?
any ideas?
Thanks
edit: the second row of numbers are based on 10 by 10 count.
so. first record will have 10 other records underneath, the second record another 10 and so on.
i've tried something using $floor= floor($i/10); and adding that to the second set of records in here: $nr=str_pad($floor, 10, "0", STR_PAD_LEFT);
I don't quite understand what you are trying to do. But one possible solution would be this:
for ($i = 1; $i < 100; $i++){
if ($i % 10 == 1)
echo str_pad(($i-1)/10, 10, "0", STR_PAD_LEFT) . "<br />\n";
echo ' '.str_pad($i, 10, "0", STR_PAD_LEFT) . "<br />\n";
}
See it in action.
Depending on your problem, using two cycles might be better for you:
for ($i = 0; $i < 10; $i++){
echo str_pad($i, 10, "0", STR_PAD_LEFT) . "<br />\n";
for ($j = 1; $j <= 10; $j++) {
echo ' '.str_pad($i*10+$j, 10, "0", STR_PAD_LEFT) . "<br />\n";
}
}
Live.
This looks like homework, but I would change your loop as described to iterate the secondary numbers, and then use a test such as MOD ( http://php.net/manual/en/internals2.opcodes.mod.php ) to check for those times when you want to emit the first level grouping.
So you adjust your str_pad line to indent as well as left pad with 0, and add a lne near the top of the loop to check if ($i % 10 == 0) (that is, if it is divisible by 10 without a remainder).
Note this may not be the fastest nor most elegant way.

Categories