这个函数应该回显所有的单打和双打数字。(例如 01 - 01 02)。
我尝试使用以下代码在每个"类别"之前放置一个标题,但它无法正常工作。
应该是这样的
单打
01,02,03
双打
01 02, 03 04, 05 06
你能帮我解决这个问题吗?谢谢!
function check_stats($todas){
$arrlength = count($todas);
for($x = 0; $x < $arrlength; $x++) {
$arrlength2 = count($todas[$x]);
if($arrlength2==1){
echo 'single'.'</br></br>';
list($a) = explode(" ", implode(" ", $todas[$x]));
echo $a; echo '</br>';
}
if($arrlength2==2){
echo 'double'.'</br></br>';
list($a,$b) = explode(" ", implode(" ", $todas[$x]));
echo $a.' '.$b; echo '</br>';
}
} //for
} //function
您正在尝试将单打和双打分开。并且您要单独打印它们。在尚未检测到所有内容的情况下,无法单独打印它们。然后,您需要检测所有类别,然后可以单独打印它们。为此,您应该在检测时保存类别,并在检测后打印保存的类别。
function check_stats($todas){
$singles = array(); // an array to save single numbers
$doubles = array(); // an array so save doubles
foreach ($todas as $toda) { // cleaner and beautifier than for
if (count($toda) == 1) { // direct compare without extra $arrlength2
$singles[] = $toda[0]; // will push $toda[0] to $singles array
} else if (count($toda) == 2) { // when put else compares less
$doubles[] = $toda[0] . ' ' . $toda[1];
}
}
// then you can do whatever with $singles and $doubles
echo 'SINGLES' . PHP_EOL;
foreach ($singles as $single) {
echo $single . PHP_EOL;
}
echo 'DOUBLES' . PHP_EOL;
foreach ($doubles as $double) {
echo $double . PHP_EOL;
}
}
编辑#1:如果有超过 2 种变量,则可以将它们保存在数组中。
function check_stats($todas){
$result_array = array();
foreach ($todas as $toda) {
$result_array[count($toda)] = implode(' ', $toda);
// the above code will save each single or double or triple or etc
// in an index in their size
}
return $result_array; // better return it and use out of function
}
$result = check_stats($todas);
// now print them
foreach ($todas as $key => $toda) {
echo $key . PHP_EOL; // print count (that is index)
foreach ($toda as $t) {
echo $t . PHP_EOL;
}
}