How to print the php algorithm

Source: Internet
Author: User
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 "
";}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.