creating grid content with repeated array keys - php

I have 15 elements and I want to order them using for() like this;
1 2 3
2 3 1
3 1 2
1 2 3
2 3 1
I tried;
for($i=0;$i<=15;$i++){
$a = 1;
if($a==1){
$a=($i%3)+1;
}elseif($a==2){
$a=(($i+1)%3);
}else{
$a=(($i+2)%3);
}
echo $a." ";
}
output is;
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1
repeating 1-2-3
rule is simple;
there should be 3 keys in row and a key can not be used in same row again.

$i = 1;
for($count=0; $count <= 5; $count++){
$k = 0;
for($j = $i; $j < 4; $j++){
echo $j. ' ';
$k++;
}
if($k < 3){
if($k == 1) $k = 3;
for($j = 1; $j < $k; $j++){
echo $j.' ';
}
}
echo '</br>';
$i++;
if($i > 3) $i = 1;
}

Related

Print triangular number patterns with loops and modulus calculation

I need to print a number pattern like this:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123
My code:
for($i=1; $i<=4; ++$i) {
for($j=1; $j<=$i; ++$j) {
echo $j;
}
echo ("<br/>");
}
for($i=2; $i<=4; ++$i) {
for($j=2; $j<=$i; ++$j) {
echo $j;
}
echo ("<br/>");
}
I don't know how to recycle to the first number after the max number is reached. Since my max number is 4, 1 should used instead of 5 (and 2 for 6 and 3 for 7).
You loop from 1 to 4, and subtract 4 if the value is bigger than 4.
This is for 2, 23, 234, 2341:
for ($i = 1; $i <= 4; $i++) {
for ($j = 1; $j <= $i; $j++) {
$value = $j + 1; // or +2, or +3
echo $value > 4 ? $value - 4 : $value;
}
echo "\n";
}
And this would generate all output within one big loop:
$max = 4;
for ($start = 0; $start < $max; $start++) {
for ($i = 1; $i <= $max; $i++) {
for ($j = 1; $j <= $i; $j++) {
$value = $j + $start;
echo $value > $max ? $value - $max : $value;
}
echo "\n";
}
}
Whenever you need circular array access, it is a good idea to implement a modulus calculation. To set this up, use a range between 0 and 3 instead of using 1 and 4. A modulus calculation can return a 0 result, so the formula must be prepared to handle this value. To adjust the value, just add 1 after the modulus calculation to generate numbers between 1 and 4.
Accumulate strings of numbers as desired and reset the string upon each start of the outer loop.
Below proves that you do not need three nested loops for this task.
Code: (Demo)
for ($i = 0; $i < 4; ++$i) {
for ($s = '', $j = 0; $j < 4; ++$j) {
$s .= ($i + $j) % 4 + 1;
echo $s . "\n";
}
}
Output:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123

PHP: for loop $i value turns zero if the number is greater

I want to turn the $i variable value to start counting from 1 if the given value is greater than 10:
here is what i am trying to achieve
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i < $givenValue; $i++) {
if ($givenValue > 10){
$i = 1;
}
echo $i."<br>";
}
?>
This is how i want my result to look like
output: 1
output: 2
output: 3
output: 4
output: 5
output: 6
output: 7
output: 8
output: 9
output: 10
output: 1
output: 2
output: 3
output: 4
output: 5
in for loop body
Any help is welcome
You can use modulo calculation to get the result you want.
I also changed your if from $givenvalue to $i as $givenvalue will "always" be 10+.
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($i > 10){
Echo $i%10 . "\n";
}else{
echo $i . "\n";
}
}
https://3v4l.org/5afc5
Another option, if that is possible for you, is to start at zero and only use modulo calculation and add one to it to get the same result.
This also means I need to stop the loop at <$givenvalue as your original code shows.
$givenValue = 15; //number of x value
for ($i = 0; $i < $givenValue; $i++) {
Echo $i%10+1 . "\n";
}
https://3v4l.org/r0sgA
A method that uses less looping is to add 10 to the loop on each iteration and create the values using range().
Then add them to the array with array_merge, and output with implode.
$givenValue = 47; //number of x value
$breakpoint = 10;
$arr=[];
For($i = $breakpoint; $i< $givenValue;){
// Add new values from 1-$breakpoint in array
$arr = array_merge($arr, range(1,$breakpoint));
$i +=$breakpoint;
}
// Loop will exit before all values been collected
// Add the rest of the values
$arr = array_merge($arr, range(1,$givenValue-($i-10)));
// Echo the values in array
Echo implode("\n", $arr);
https://3v4l.org/jGsO4
Your code can be written like this:
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++)
{
if ($i > 10)
{
$i = 1;
$givenValue-=10;
}
echo "output: $i\n";
}
?>
http://sandbox.onlinephpfunctions.com/code/ed34d8dcd12a9a5a866b73338ad1209f55298519
You are resenting the counter, I would expect the behaviour you have. To do what you want add another counter to the mix
$j=1;
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($j > 10){
$j = 1;
}
echo $j."\n";
++$j;
}
You also had several missing ;
Output:
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
If you want to end on 5 you have to do 16 as the $givenValue or change it to <= less than or equal
See it here live
See what I have now, the $i variable counts to the $givenValue then the $j variable counts along side it, but with a range of 1-10 ( resets to 1 after 10 )

How to make this Number Pyramid Pattern

is it possible if I want an output like this?
5 4 3 2 1
4 3 2
3
with PHP, this is the code that I've been Try.
$n = 3;
for ($i = 3; $i > 0; $i--) {
for ($j = $n - $i; $j > 0; $j--)
echo "  ";
for ($j = 2 * $i - 1; $j > 0 ; $j--)
echo " ".$j;
echo "<br>";
}
and I got this , from that code
5 4 3 2 1
3 2 1
1
Wich part of my code that I do get wrong? can someone help me?
EDIT: Thanks people. most of the stackoverflow question that similar to my question is like my result that I have. the task was not that. 54321 432 3
most of them we're like 54321 4321 321 21 1. I'm sorry , I'm newbie. don't know that much as you people. once again , Thanks Alot!
You need to fix your last for loop's initial value and condition to match your criteria:
$n = 3;
for ($i = 3; $i > 0; $i--) {
for ($j = $n - $i; $j > 0; $j--)
echo " ";
for ($j = $n + $i - 1; $j > $n - $i ; $j--)
echo " ".$j;
echo "<br>";
}

PHP nested while loop

I tried to do a while inside a while to print a multiplication table like,
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
But I got only 1, 2, 3, 4, 5.
Code:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
This is happening because you're not resetting $x when the inner loop completes its iteration. Try this instead:
$i = 1;
while($i <= 5) {
$x = 1;
while($x <= 5) {
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
You need to reset $x, so:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
$x = 1; // added this line
echo "<br>";
$i++;
}
Output:
12345
246810
3691215
48121620
510152025
You can then do what ever you want to format it.
More elabrate explanation:
First run:
It enters both outer and inner loops, showing the desired output for the first line. You end up with $i = 2 and $x = 6.
Second run:
Since $i is 2, it doesn't leave the outer loop, but $x is 6, so it doesn't enter the inner loop again.
Last* run:
It then keeps adding 1 to $i until it doesn't match the outer loop condition anymore and leaves you with that unwanted result.
Use this
This is because you have not initialized your $x after external while loop completes its one cycle. so after one cycle inner loops does not run
<?php
$i = 1;
while($i <= 5) {
$x = 1;
while($x <= 5) {
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
DEMO ONLINE
php code:
$i = 1;
while($i <= 5){
$x = 1;
while($x <= 5){
echo $i * $x." ";
$x++;
}
echo "<br/>";
$i++;
}
result:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

How to produce the following sequence of numbers through a php for statement

I need to find out a way to output the following number sequence through a double php for statement:
0 - 1 <-- begin
0 - 2
0 - 3
0 - 4
1 - 1
1 - 2
1 - 3
1 - 4
2 - 2
2 - 3
2 - 4
3 - 3
3 - 4 <-- end
How can I make the second loop, lose its first number, after each loop so the sequence looks like the output above?
I've been trying things like:
for($z = 0; $z <= 3; $z++) {
for($y = 0; $y <= 3; $y++) {
echo $z . " - " . $y . "<br />";
}
}
But the second loop keeps starting with a number 1. But I can't even think of a way to do it at all.
for ($x = 0; $x <= 3; $x++) {
for ($y = 1; $y <= 4; $y++) {
if ($y >= $x) {
echo "$x - $y\n";
}
}
}
Use a nested loop and let the nested one start at the iteration of the outer one:
for ($i = 0; $i < $max; $i++)
for ($j = $i; $j < $max; $j++)
echo "$i - $j";

Categories