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.
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments