$all_categories=get_cats($cat);
$headingPrinted = false;
$childCount = 0;
for($i=0;$i<sizeof($all_categories);$i++)
{
$arr=get_gender($cat);
if($arr[$i]=='0')
{
if (!$headingPrinted) {
echo "  "."Sons:";
$headingPrinted = true;
}
echo "  ".$all_categories[$i].",";
$childCount++;
}
}
if ($childCount >= 3) {
echo $childCount, $childCount == 1 ;
}
else {
// you may want to do something if none found
}
$headingPrinted = false;
$childCount = 0;
for($i=0;$i<sizeof($all_categories);$i++)
{
$arr=get_gender($cat);
if($arr[$i]=='1'){
if (!$headingPrinted) {
echo "  "."Daughters:";
$headingPrinted = true;
}
echo "  ".$all_categories[$i].",";
$childCount++;
}
}
if ($childCount >= 3) {
echo $childCount, $childCount == 1 ;
}
else {
// you may want to do something if none found
}
我正在显示“儿子”这个词,然后是他们的名字,然后他们的数字和“女儿”这个词
那么他们的名字就算了。我希望像这样显示
儿子:a,b,c,d:4;
女儿:p,q,r,s:4;
$ all_categories是一个数组,其中我得到所有给定id的孩子,get_gender和get_cats是函数。
现在我显示 a,b,c,d,:4; 。我只想在数组的最后一个元素之后删除逗号 我只想在儿子的数量大于2且女儿的数量相同的情况下才显示计数 如果男孩数是1,那么“儿子:”一词;女儿也一样 如果男孩数是2,那么“儿子:”a,b;女儿也一样 如果男孩数大于2,那么“儿子:”a,b,c:3;女儿也一样 我想显示与上面相同的结果我说。所以任何人都可以尝试这个???
答案 0 :(得分:1)
要删除显示儿子和女儿的最后一个逗号,检查下一条记录是否存在然后附加逗号,否则转义逗号。
$all_categories=get_cats($cat);
$headingPrinted = false;
$childCount = 0;
for($i=0;$i<sizeof($all_categories);$i++)
{
$arr=get_gender($cat);
if($arr[$i]=='0')
{
if (!$headingPrinted) {
echo "  "."Sons:";
$headingPrinted = true;
}
echo "  ".$all_categories[$i];
if(isset($all_categories[$i+1])){
echo ",";
}
$childCount++;
}
}
if ($childCount >= 3) {
echo $childCount, $childCount == 1 ;
}
else {
// you may want to do something if none found
}
$headingPrinted = false;
$childCount = 0;
for($i=0;$i<sizeof($all_categories);$i++)
{
$arr=get_gender($cat);
if($arr[$i]=='1'){
if (!$headingPrinted) {
echo "  "."Daughters:";
$headingPrinted = true;
}
echo "  ".$all_categories[$i];
if(isset($all_categories[$i+1])){
echo ",";
}
$childCount++;
}
}
if ($childCount >= 3) {
echo $childCount, $childCount == 1 ;
}
else {
// you may want to do something if none found
}