decrese value of loop - php

I have this code, but i want in second loop a decrease of $p value. The first internal loop must be repeated three times, the second, two times and the last, one time. I am trying $p-- but without success.
Any idea ? thanks
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
$p--;
}
}

Move your $p-- to outside the inner for loop:
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
}
$p--;
}
Or better, just depend on the value of $i:
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < 3 - $i; $o++) {
echo "something";
}
}
Or if you don't actually use $i:
for ($i = 2; $i >= 0; $i--) {
for ($o = 0; $o < $i; $o++) {
echo "something";
}
}

It's quite simple.
for ($i = 2; $i >= 0; $i--)
{
}
Set the starting number at the upper limit number, and then go down until equal to 0, $i minus 1;

You need to decrement $p outside the first loop

Related

How can received and clear an array in PHP

I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

for loop gets executed just once

I have created an array dynamically like this way
$names = array();
for ($i = 0; $i < 100; $i++) {
$names[] = $i;
}
then created part
$parts = count($names) / 20;
and created a sub array then loop through the parts
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i = 0; $i < count($newarray); $i++) {
echo $i;
}
$j = $j + 20;
}
The problem is that this code displays from zero to 19 It doesn't display the other parts
Both the inner and outer loops use the same control variable $i, so just change the inner one...
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i1 = 0; $i1 < count($newarray); $i1++) {
echo $i1;
}
$j = $j + 20;
}

PHP Generate possible number with specific format

I need to generate possible number with some format/rule.
5 digit number
first digit > 0
for($x=0;$x=10;$x++) {
$first=1;$second=0;$third=0;$fourth=0;$five=0;
for($i=1;$i<10;$i++){
$first=$i; break;
}
for($i=0;$i<10;$i++){
$second=$i; break;
}
for($i=0;$i<10;$i++){
$third=$i; break;
}
for($i=0;$i<10;$i++){
$fourth=$i; break;
}
for($i=0;$i<10;$i++){
$five=$i; break;
}
echo $first.$second.$third.$fourth.$five.'\n';
}
desired result:
10000
10001
10002
...until
99999
but seems broken :(
Instead of using separate for loops you need to nest them inside one another:
for($i = 1; $i <= 9; $i++){
for($j = 0; $j <= 9; $j++){
for($k = 0; $k <= 9; $k++){
for($l = 0; $l <= 9; $l++){
for($m = 0; $m <= 9; $m++){
print("$i$j$k$l$m\n");
}
}
}
}
}
DEMO
But, obviously, a simpler approach would employ only one for loop:
for($i = 10000; $i <= 99999; $i++){
print("$i\n");
}
DEMO

add in loop in multi dimensional array

i am facing a problem
can some one suggest me
for ($i = 1; $i <= 2; $i++) {
$r2 = 0;
for ($t = 1; $t <= 2; $t++) {
echo $r2;
$r2++
}
}
output is 0101;
can i get output 0123 ??? please
if
for ($i = 1; $i <= 3; $i++) {
$r2 = 0;
for ($t = 1; $t <= 3; $t++) {
echo $r2;
$r2++
}
}
output is 010101;
can output 012345678 ??? please
and if
for ($i = 1; $i <= 4; $i++) {
$r2 = 0;
for ($t = 1; $t <= 4; $t++) {
echo $r2;
$r2++
}
}
output is 01010101;
can output 0123456789101112131415 ??? please
i think you understand
thanks
In all of these cases you are initializing $r2=0; in the inner loop. It should be outside the loop.
$r2=0;
for($i=1;$i<=2;$i++){
for($t=1;$t<=2;$t++){
echo $r2;
$r2++
}
}
This would produce "1234".
why are you using two nested for loops ?
why not just use one:
for ($i=0; $i<=15; $i++) echo $i . " ";
Try this:
$r2 = 10;
for($t = 0; $t <= $r2; $t++){
echo $r2;
}
Oh wait.. I get it now, why you have the two nested loops, you want to essentially raise a number to the power of 2 in order to control the number of values output. In that case, what you want is simply this:
// this is the variable you need to change to affect the number of values outputed
$n = 2;
// Square $n
$m = $n * $n;
// Loop $m times
for ($i = 0; $i < $m; $i++) {
echo $i;
}

Refactoring 2 similar if statements

Would you write this code any differently? I don't mind tertiary operator by the way so any ideas that include it are also welcome
if ($stopYear < $startYear) {
for ($i = $startYear; $i >= $stopYear; $i--) {
$yearMultiOptions[$i] = $i;
}
} else {
for ($i = $startYear; $i <= $stopYear; $i++) {
$yearMultiOptions[$i] = $i;
}
}
$min = min($startYear, $stopYear);
$max = max($startYear, $stopYear);
for ($i = $min; $i <= $max; $i++) {
$yearMultiOptions[$i] = $i;
}
I don't know php, so min and max might have different syntax, but you get the idea.
You could do something like this:
$step = ($stopYear < $startYear) ? -1 : 1;
for ($i = $startYear; $i != $stopYear; $i += step) {
$yearMultiOptions[$i] = $i;
}

Categories