数组的回型输出

<?php
/**
 * Created by PhpStorm.
 * Date: 2017/4/11
 * Time: 14:02
 */
class test1{

    public  function outArray($str){
        $length = sizeof($str[0]);
        $height=sizeof($str);
        $total = $length * $height;
        $flag = 1;

        while($total){
            //
            for($i = $flag -1;$i <= $length -$flag;$i++ ) {
                print($str[$flag - 1][$i]);
                $total--;
                if ($total == 0) {
                    return 0;
                }
            }
                //
                for($j = $flag;$j<=$height-$flag-1;$j++){
                    print($str[$j][$length-$flag]);
                    $total--;
                    if($total==0){
                        return 0;
                    }
                }
                //
                for($m = $length -$flag;$m>$flag-1;$m--) {
                    print($str[$height - $flag][$m]);
                    $total--;
                    if ($total == 0) {
                        return 0 ;
                    }
                }
                //
                    for($n = $height-$flag;$n>=$flag;$n--){
                        print($str[$n][$flag-1]);

                        $total--;
                        if($total==0){
                            return 0;
                        }
                    }

                $flag++;
            }
        }




}

$a = array(
    array(1,2,3,4),
    array(12,13,14,5),
    array(11,16,15,6),
    array(10,9,8,7)
);
$c = array(
    array(1,2,3),
    array(8,9,4),
    array(7,6,5)
);
$b = new test1();
$b->outArray($a);

 

posted @ 2017-04-17 17:07  木子点点  阅读(503)  评论(0编辑  收藏  举报