$p&&$c++,$t[$c][$i]=$z=$p,$i++);ksort($t); foreach($t as $v){for($i=0,$l='';$i<$q;$l.=isset($v[$i])?$v[$i]:' ',$i++);echo rtrim($l)."\n";}