vertical to horizontal number move in php(How do I distribute values of an array in three columns?)

1
290

Reverce string
<!--?php $s = 'abcdefghijklm';
for($i=strlen($s)-1, $j=0; $j<$i; $i--, $j++) { list($s[$j], $s[$i]) = array($s[$i], $s[$j]); } echo $s;?>
Row and colume display data
<!--?php
//coulem represent
function print_values_table($array, $maxCols = 3, $format = "%-4d") {
$values = array_values($array);
$count = count($values);
$lines = ceil($count / $maxCols);

for($line = 0; $line < $lines; $line++) {
if($line !== 0) echo ”
“;

for($i = $line; $i < $count; $i += $lines) { printf($format, $values[$i]); } } } $values = range(1,10); print_values_table($values); echo ” colume represent”; $a = array(1,2,3,4,5,6,7,8,9,10,11,12,13); $row_length = 3; // change to vary length of rows foreach( $a as $index => $value) {
//echo $index%$row_length.”-“;
if($index%$row_length == 0) echo ”
“;
echo “$value “;
}
?>

<!–?php //factorial value
function factorial($number) {

if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}
echo factorial(6);

//row colume value

//coulem represent
function print_values_table($array, $maxCols = 3, $format = “%-4d”) {
$values = array_values($array);
$count = count($values);
$lines = ceil($count / $maxCols);

for($line = 0; $line < $lines; $line++) {
if($line !== 0) echo ”
“;

for($i = $line; $i < $count; $i += $lines) { printf($format, $values[$i]); } } } $values = range(1,10); print_values_table($values); echo ” colume represent”; $a = array(1,2,3,4,5,6,7,8,9,10,11,12,13); $row_length = 3; // change to vary length of rows foreach( $a as $index => $value) {
if($index%$row_length == 0) echo ”
“;
echo “$value “;
}

echo “second way
“;
$arr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15, 16);
$max = count($arr);
$cols = 3;
$block = ceil($max / $cols);
for ($i = 0; $i < $block ; $i++) {
echo $arr[$i] . ‘ ‘;

for ($j = 1; $j < $cols; $j++) { $nexKey = $i + $block * $j; if (!isset($arr[$nexKey])) break; echo $arr[$nexKey] . ‘ ‘; } echo ” “; } ?>
?>

<!–?php //verticle
$target = array(1,2,3,4,5,6,7,8,9,10);
$target = array_chunk($target, 3); //Split the array into chunks of 3.
$result = array_map(function ($el) {
return implode(“”, $el); //Implode the inner arrays with table cells
}, $target);
$result = implode(”

“, $result); //Implode the outer arrays with table rows.

echo ”

“;
echo $result;
echo “

“;

//horizontal
$q = 10;//number of questions
$c = 3;//number of columns
$cell = ‘

i

‘;
$table = ‘

‘; $mod = array_fill(0, $c, 0); for ($i=0; $i<($q%$c); $i++){ $mod[$i] = 1; } $step = floor($q/$c); $arr = array(); $cum = 0; $table .= ”; for ($i=0; $i<$c; $i++){ $cum += @$mod[$i-1]; $num = 1 + $step*$i + $cum; $arr[] = $num; $table .= str_replace(‘i’, $num, $cell); } $table .= ”; $next = $arr; for ($i=1; $i $value){ $num = ++$next[$key]; $table .= str_replace(‘i’, $num, $cell); } //$arr = array_merge($arr, $next); $table .= ”; } $table = substr($table, 0 , -4); for ($i=0; $i $value){ $ini = ”; $last .= str_replace(‘i’, $value, $cell); $fin = ”;
}
//$arr = array_merge($arr, $next);
$table .= $ini.$last.$fin.’

‘;
echo $table;

function factorial($number) {

if ($number < 2) { return 1; } else { return ($number * factorial($number-1)); } } echo factorial(6); ?>

1 COMMENT