Hey all i can not solve this,that's why i post it here .i am a php larner and trying solve small php problems.my problems are below.
0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
how to print the following pattern ?
image link-http://imgur.com/4Y5L8ZZ
This worked. I'm not sure whether it's the best approach.
for ($i = 0; $i < 5; $i++) {
for ($a = $i; $a > 0; $a--) {
echo $a;
}
for ($b = 0; $b <= $i; $b++) {
echo $b;
}
echo "\r\n";
}
<?php
for ($out = 0; $out < 5; $out++) {
for ($row = $out; $row > 0; $row--) {
echo $row . " ";
}
for ($col = 0; $col <= $out; $col++) {
echo $col. " ";
}
echo "<br>";
}
?>
Please try executing following code snippet as a solution according to above mentioned description.
$rows=5;
for($i=0;$i<$rows;$i++)
{
echo "<br>";
for($j=$i;$j>=0;$j--)
{
echo "\t".$j;
}
for($k=1;$k<=$i;$k++)
{
echo "\t".$k;
}
}
Related
I want to print number from 1 to 12 in matrix form and expected output is:
1 5 9
2 6 10
3 7 11
4 8 12
code:
<?php
for ($i=1; $i<=12; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i." ";
}
echo "<br/>";
}
?>
I have got wrong output. So, How can I get expected output as I mention above? Please help me.
Thank You
Some "magic" code):
foreach (range(1,4) as $num) {
echo implode(' ', range($num,12,4)) . '<br />';
}
Version with for:
for ($i = 1; $i <= 4; $i++) {
for ($j = $i; $j <= 12; $j +=4) {
echo $j . ' ';
}
echo '<br />';
}
$z=0;
for ($x = 1; $x <= 4; $x++)
{
echo " $x ";
$z=$x;
for ($y = 1; $y <= 2; $y++)
{
$z=$z+4;
echo " $z ";
}
echo "\n";
}
$maxRow = 4;
$maxColumn = 3;
for ($row = 1; $row <= $maxRow; $row++)
{
for ($column = 1; $column <= $maxColumn; $column++) {
$number = $row + 4*($column-1);
echo $number." ";
}
echo "<br/>";
}
should work
foreach (range(1, 4) as $res) {
echo implode(' ', range($res, 12, 4));
echo "<br>";
}
<?php
$count=count($admissions);
$divide=$count/3;
$divide=round($divide);
foreach($admissions as $key => $row)
{
if(//First Part )
{
echo "Alpha";}
else if(//2nd Part )
{
echo "Beta";
}else
{
echo "Gamma";
}
}
?>
I have a dynamic array list and i want to divide it equally in 3 parts.
if Count of array is 30.
So i want to echo for first 10 record
echo "Alpha";
Second 10 Records
Echo "Beta";
3rd 10 Records
Echo "Gamma";
if array size is 60 then it will be divided into 20 parts each.
How can i echo the alpha, beta and gamma.
I thing your question is about if conditions. So you can use this code:
$count=count($admissions);
$divide=$count/3;
$divide=round($divide);
$i = 1;
foreach($admissions as $key => $row)
{
if($i > 0 && $i <= $divide)
{
echo "Alpha";
}
else if($i > $divide && $i <= ($divide*2))
{
echo "Beta";
}
else //equal else if($i > $divide*2 )
{
echo "Gamma";
}
$i++;
}
Try using a normal for loop :
For ($i = 0; $i < $count ; $i++){
echo "alpha";
}
For ($i = $count; $i < 2*$count ; $i++){
echo "beta";
}
For ($i = 2*$count; $i < 3*$count ; $i++){
echo "gamma";
}
Try this hope you are expecting this. According to the requirement which you specified in comments.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$range=range(0,12);
$result=array_chunk($range, 4);
if(count($result[count($result)-1])!=4)
{
$temp=$result[count($result)-1];
unset($result[count($result)-1]);
$result[count($result)-1]=array_merge($result[count($result)-1],$temp);
}
print_r($result);
Let's have an array of three elements and play with the modulo:
<?php
$count=count($admissions);
$divide=$count/3;
$divide=round($divide);
$divisions = array(0 => array(), 1 => array(), 2 => array())
$modulo = 0;
foreach($admissions as $key => $row)
{
$divisions[($modulo + 1) % 3][$key] = $row;
}
This will do the partitioning you need.
How can i print output like this?
1 2 3 4
5 6 7 8
9 10
i had tried this code but not luck.
for ($i = 1; $i<=10; $i++)
{
if ($i <= 4)
{
echo $i;
if ($i >= 4){
echo'<br>';
for($x = $i; $x<=10; $x++){
echo $x;
}
}
}
}
this code output look like this.
1234
45678910
Try this
You need to check the value of $i is module by 4 or not, if then echo a break.
for ($i = 0; $i< 10; $i++){
if ($i % 4 == 0)
echo'<br>';
echo ($i + 1)." ";
}
Or
for ($i = 1; $i<= 10; $i++){
echo $i." ";
if ($i % 4 == 0)
echo'<br>';
}
Output
1 2 3 4
5 6 7 8
9 10
The idea is if the no. of elements in a row is a multiple of 4, then there's a line break.i.e for 4, 8, 12, 16...there will be line breaks.
Try this:
for ($i = 1; $i <= 10; $i++) {
echo $i;
if ($i % 4 == 0) {
echo "<br>";
}
}
Here is your code
for ($i = 1; $i<=10; $i++)
{
echo $i.' ';
if ($i % 4==0){
echo'<br>';
}
}
Output
1 2 3 4
5 6 7 8
9 10
Try this
for ($i = 1; $i<=10; $i++)
{
echo $i.' ';
if ($i % 4 == 0)
{
echo'<br>';
}
}
Output
1 2 3 4
5 6 7 8
9 10
Try this, you need to just check if the number is completely divisible by 4 or not
<?php
for ($i = 1; $i<=10; $i++)
{
echo $i.' ';
if ($i % 4 == 0) {
echo'<br>';
}
}
?>
also you can find a working example here http://phpfiddle.org/main/code/ae6y-3749
<?php
$numbers = range(1, 10);
$chunks = array_chunk($numbers, 4);
foreach($chunks as $chunk){
foreach ($chunk as $number){
echo "$number ";
}
echo "<br>\n";
}
My sample desired output should be
1 2 3 4 5
2 4
3 3
4 2
5 4 3 2 1
Here is my PHP code
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=0;$y<(5-$i)*4;$y++) {
echo ' ';
}
for($l=$i;$l>0;$l--) {
echo "$l";
}
echo "<br/>";
}
But I got this output.
output:-
1 1
12 21
123 321
1234 4321
1234554321
Please try to solve my problem. Thanks in advance.
for($i=1; $i<=5; $i++){
echo $i." ";
}
echo "<br />";
for($i=2; $i<=5; $i++){
if($i==5){
echo $i;
}
else{
echo $i." ";
if($i==2){
echo (4)."<br />";
}
if($i==3){
echo (3)."<br />";
}
if($i==4){
echo (2)."<br />";
}
}
}
echo " ";
for($i=4; $i>=1; $i--){
echo $i." ";
}
#Mark has the best solution, I guess.
Here's a quick solution for an arbitrary array of 1-character values:
$values = range(1,7);
$count = count($values);
foreach($values as $k=>$v) {
if($k == 0)
echo implode(" ", $values), "\n";
elseif($k == $count-1)
echo implode(" ", array_reverse($values)), "\n";
else
echo $v, " ", str_repeat(" ", $count-2), $values[$count-1-$k], "\n";
}
This will produce:
1 2 3 4 5 6 7
2 6
3 5
4 4
5 3
6 2
7 6 5 4 3 2 1
$count = 5;
$last = 0;
for ($i = 1; $i <= $count; $i++) {
if($i == 1) {
for ($x = 1; $x <= 5; $x++) {
echo $x . ' ';
}
$last = $x;
} elseif ($i == 5) {
for ($b = 5; $b >= 1; $b--) {
echo $b . ' ';
}
} else{
for($c=1; $c <= 5; $c++) {
if($c == 1) {
echo $i . ' ';
} elseif ($c == 5) {
echo ($last - $i) . ' ' ;
} else {
echo ' ';
}
}
}
echo '<br>';
}
How to loop with sample input = 5
and the output :
1 2 3 4 5
0 2 3 4 5
0 0 3 4 5
0 0 0 4 5
0 0 0 0 5
PHP:
<?php
$i=5;
for($a=1; $a<=$i; $a++){
echo $a." ";
}
echo "\n";
for($a=0; $a<=$i; $a++){
if($a==1){
continue;
}
print "$a ";
}
echo "\n"; $ex = array(1,2);
for($a=1; $a<=$i; $a++){
if(in_array($a, $ex)){
continue;
}
print "$a ";
}
?>
How to solve this issue?
Using built-in functions, it's easier to read and understand:
$input = 5;
$nums = range(1, $input);
for ($zeros_count = 0; $zeros_count < $input; $zeros_count++) {
echo str_repeat('0 ', $zeros_count);
echo implode(' ', array_slice($nums, $zeros_count)) . PHP_EOL;
}
Think simple
<?php
$input = 5;
for($i = 1; $i <= $input; $i++ ) {
for($j = 1; $j <= $input; $j++) {
if( $i > $j) {
echo "0 ";
} else {
echo $j . " ";
}
}
echo "<br>";
}
?>