<?php header("Content-type: text/html; charset=utf-8"); $count = 19;//5 9另外处理 $tempArray = array(2,3,4); $_template = array(); if($count >= 2){ if(in_array($count, $tempArray)){ array_push($_template, $count); }else{ if($count == 5){ array_push($_template, 2); array_push($_template, 3); }else if($count == 9){ array_push($_template, 4); array_push($_template, 2); array_push($_template, 3); }else{ Calculate($_template, $count, $tempArray); } } }else{ array_push($_template, $count); } function Calculate(&$_template, $count, $tempArray) { shuffle($tempArray); while($count>=2) { $templateid = array_rand($tempArray,1); $templateValue = $tempArray[$templateid]; #echo "模板值为:{$templateValue}<BR><BR>"; if($count >= 2){ if($count >= $templateValue){ $count = $count - $templateValue; #echo "count:{$count}<BR><BR>"; array_push($_template, $templateValue); //echo $templateid."<BR><BR>"; }else{ array_push($_template, $count); $count = 0; } } } if($count == 1){ $isZero = false; #echo "最后还有一个哦{$count}<BR><BR>"; if(count($_template) >= 1){ foreach($_template as $key=>$val){ if($val <= 3 ){ #echo "最后一个处理完成"; $_template[$key] = $val+1; $isZero = true; break; } } } if($isZero == false){ #echo "<h1>只能添加一个模板了</h1>"; array_push($_template, $count); } } } echo "<BR><BR><BR>最后取得的模板数组为:<BR><BR>"; var_dump($_template); ?>