How to print the php algorithm is occasionally seen, so I printed it in php.
There are two ways to solve this problem:
Method 1: According to the graph analysis, the graph is a two-dimensional array, which can be solved by two-dimensional arrays. The six rows are six arrays, and each row represents the number of elements in the array. If you know the number of elements in each array, do you know which elements are in the array?
function array_chunk_vertical($arr, $colun){ $arr_length = count($arr); $parem = floor($arr_length / $colun); $pare = $arr_length % $colun; $ar = array(); for ($i = 0; $i < $colun; $i ++) { $par = $i == $colun - 1 ? 1 : 5; $ar[] = $par; } $newarray = array(); foreach ($ar as $value) { for ($i = 0; $i < $value; $i ++) { $newarray[$i][] = array_shift($arr); } } return $newarray;}$arr = range(1, 31);$data = array_chunk_vertical($arr, 7);foreach ($data as $value) { foreach ($value as $va) { printf('[%2s]', $va); } echo "
";}
Method 2: for example, the code first gives an empty array of size and fills in the element in the empty array. The specific method is as follows:
$value) { if ($preserve_keys) { $chunks[$chunk_index][$key] = $value; } else { $chunks[$chunk_index][] = $value; } if (++$chunk_index == $chunk_count) { $chunk_index = 0; } } return $chunks;}$data=range(1,31);$newdata=array_chunk_vertical($data,6);foreach ($newdata as $root){ foreach ($root as $val){ printf('[%2s]',$val); } echo "
";}