How to range and sequence in PHP - php

i need a sequence like this in a range from 0 to 10 with PHP. The result would be:
0
1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
10
........
I want to insert this function inside a foreach
UPDATE
<?php $count = 0; ?>
<div class="<?php echo 'minipost-'. $count++ ; ?>">
<?php
get_template_part( 'content', get_post_format() );
?>
</div>

You already mentioned the solution in your question. You can use:
range(0, 10)
To repeat the sequence 3 times you could do:
str_repeat(implode(' ',range(0, 10)).' ', 3);

Without foreach;
$iCounter = 2;
$iCount = 10;
for ($k = 1; $k <= $iCounter; $k++) {
for ($i = 0; $i <= $iCount; $i++) {
// do anything you need
echo $i;
}
}

Related

Pascals Diagonal Triangle in PHP

I'm trying to use php code to print out pascals triangle (in the diagonal style like this- http://www.cut-the-knot.org/arithmetic/combinatorics/PascalTriangle.gif)
I tried this code:
<?php
$f = 10;
for ($x = 0; $x <= $f; $x++) {
echo "1"." ";
$previous_line[$x]=1;
}
echo "<br>";
for ($x = 0; $x < $f; $x++) {
echo "1"." ";
for ($y = 1; $y <= $f-$x-1; $y++) {
$sum = 0;
for ($z = 0; $z <= $y; $z++) {
$sum = $sum + $previous_line[$z];
}
echo $sum." ";
}
echo "<br>";
}
But I get this output:
1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
What am I doing wrong?
I think you use same $previous_line[$] value for every line, so the the iteration value of $sum will increased constantly. (increased by 1)
You should update $previous_line[$] value on every line:
$previous_line[$y] = $sum;
and you don't need to use this iteration:
for ($z = 0; $z <= $y; $z++) {....}
This is full code:
<?php
$f = 10;
for ($x = 0; $x <= $f; $x++) {
echo "1"." ";
$previous_line[$x]=1;
}
echo "<br>";
for ($x = 0; $x < $f; $x++) {
$sum = 1;
echo $sum." ";
for ($y = 1; $y <= $f-$x-1; $y++) {
$sum = $sum + $previous_line[$y];
echo $sum." ";
$previous_line[$y] = $sum;
}
echo "<br>";
}
Just try it
Since Justin beat me to the punch, I though I would post an improved version.
Please note that there is probably better ways to do this.
I removed your first loop since it wasn't need, then I moved $previous_line inside the second loop and checked to make sure that it is being set. Last I update $currentSum and assign
$totalToLoop = 10;
for ($x = 0; $x <= $totalToLoop; $x++) {
$currentSum = 1;
echo '1 ';
for ($y = 1; $y <= ($totalToLoop - $x); $y++) {
if (!isset($previous_line[$y])) {
$previous_line[$y] = 0;
}
printf('%d ', $currentSum = ($currentSum + $previous_line[$y]));
$previous_line[$y] = $currentSum;
}
echo '<br>';
}
results.
1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45
1 4 10 20 35 56 84 120
1 5 15 35 70 126 210
1 6 21 56 126 252
1 7 28 84 210
1 8 36 120
1 9 45
1 10
1

How to use nested for() to get results intermittently and decreased?

I want this output:
1 1 1 1 1 1
2 2 2 3 3 3
4 4 5 5 6 6
7 8 9 10 11 12
I think I need to three nested For(), But I don't know how should I print the above result. Here is my code, How to complete it? (though I don't know, maybe my code is completely wrong)
for ($i=1; $i<=4; $i++) // row
{
for ($j=1; $j<=6; $j++) // column
{
for($z=1; $z<=12; $z++) // number
{
// what should be in here?
}
}
}
Edit: I want something like these examples: (Although these examples are very simple, what I want is a little more harder)
for ($i=1; $i<=4; $i++)
{
for ($j=1; $j<=6; $j++)
{
echo $i.' ';
}
echo '<br>';
}
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Or this: echo $j;
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Edit2:
Note: I need to a code that be able to print this either: (its logic is the same with first output)
4 4 4 4 4 4
5 5 5 6 6 6
7 7 8 8 9 9
10 11 12 13 14 15
You can try something like this.
var $c = 1;
for ($i=1; $i<=4; $i++)
{
var $noOfChanges = 6/$i;
for ($j=1; $j<=6; $j++)
{
echo $c.' ';
if($j%$noOfChanges==0){
$c = $c + 1;
}
}
echo '<br>';
}
Not tested.
You can intialize the var $c = 4; to get the next pattern.
Tested and working:
$length = 6;
$row = 0;
$number = 1;
$total = 0;
$n = $length;
while(true) {
$n = floor($length/($row+1));
for($i = 0; $i<$n; $i++) {
echo $number;
echo "\t";
}
$total+=$n;
if($total >= $length) {
$row++;
$total = 0;
echo "\n";
if($n == 1 ) break;
}
$number++;
}

How can I make this function work with recursion?

Output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
function Triangle ($begin, $end) {
if ($begin < 0 || $end < 0) {
return;
}
if ($begin == $end) {
return $a;
}
else {
// non recursive
for ($i = 1; $i <= $end; $i++) {
for ($j = $begin; $j <= $i; $j++) {
echo $j . " ";
}
echo "<br>";
}
}
}
This is what I made so far.
Here's one way:
function triangle ($begin, $end, $row = 1) {
//stop when we've printed up to end
if($end - $begin + 1 < $row) return;
//let's start at the beginning :)
for($i = 0; $i < $row; $i++){
//the row number increments each time so we can keep adding.
echo ($begin + $i)." ";
}
echo "<br>";
//now recurse...
triangle($begin, $end, $row + 1);
}
Usage:
triangle(3,9);
Output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
3 4 5 6 7 8 9
This should work for you:
(Here I just added the variable step which defines how many steps you make from $begin to $end and if $begin + $step == $end the function is done. If not It starts from $begin and makes X steps and as long as it doesn't reach the end I call the function again with a step more)
<?php
function Triangle($begin, $end, $step = 0) {
for($count = $begin; $count <= ($begin+$step); $count++)
echo "$count ";
echo "<br />";
if(($begin + $step) == $end)
return;
else
Triangle($begin, $end, ++$step);
}
Triangle(3, 8);
?>
output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8

How to group elements 5 by 5?

I have an array of about 40-something elements that I need grouped 5 by 5 like this:
<div class="five">
1
2
3
4
5
</div>
<div class="five">
6
7
8
9
10
</div>
What would be an elegant solution?
Just call array_chunk:
$numbers = range(1, 10);
$split = array_chunk($numbers, 5);
Try:
<?php
$i = 0;
foreach($array as $value){
if($i % 5 == 0){echo '<div class="five">';}
echo $value;
if($i % 5 == 4){echo '</div>';}
$i++;
}
?>

display format of input using php

I need to display array of 10 numbers in three columns. if i add another number some like, 11 it must add below 10. as numbers increasing row can be increased not the column, can any one say?
1 4 7 10
2 5 8
3 6 9
am getting 10 in fourth column, but i need it in third column. and row will get increased like
1 4 8
2 5 9
3 6 10
4 7
Display Output in table..
<?php
$arr = array("1","2","3","4","5","6","7","8","9","10", "11", "12", "13");
$row= ceil(count($arr)/3);
echo "<table border='1'>";
for($i = 1; $i <= $row; $i++) {
echo "<tr>";
echo "<td>". $i ."</td>";
$k = 0;
$pre = 0;
for($j = 1; $j <= 2; $j++) {
if($pre == 0)
$pre = $k = $i + $row;
else
$pre = $pre + $row;
if($pre <= max($arr))
echo "<td>". $arr[$pre-1] ."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Output will be:
1 6 11
2 7 12
3 8 13
4 9
5 10
Try this...
<?php
$arr = array("1","2","3","4","5","6","7","8","9","10", "11", "12", "13", "14");
$row= ceil(count($arr)/3);
for($i = 1; $i <= $row; $i++) {
echo $i;
$k = 0;
$pre = 0;
for($j = 1; $j <= 2; $j++) {
if($pre == 0)
$pre = $k = $i + $row;
else
$pre = $pre + $row;
if($pre <= max($arr))
echo " ". $arr[$pre-1] ." ";
}
echo "<br>";
}
?>
Output when 14 element:
1 6 11
2 7 12
3 8 13
4 9 14
5 10
Output when 11 element:
1 5 9
2 6 10
3 7 11
4 8
Output when 13 element:
1 6 11
2 7 12
3 8 13
4 9
5 10

Categories