如果count(数据)低于或等于4,则打印一个或打印两个 - 不工作

时间:2012-04-11 19:31:38

标签: php if-statement

我需要一些帮助才能直接思考。我有这个代码来计算forecast_conditions的正确数量,即4; (count($condition) -1) <= 4 ? ', ' : ', eller '

count($condition)打印5所以我必须在那里有减号。 $condition位于foreach,因此我可以将内容循环到forecast_conditions; foreach($whome_answer[weather][forecast_conditions] AS $condition) {

上面的代码打印onsdag, torsdag, fredag, lördag,,这是错误的。这就是我想要的方式:onsdag, torsdag, fredag, eller lördag但我无法在代码中正确使用它!我测试了各种解决方案(== 4,&gt; = 4,!= 4,&lt; 4等等)。

如何让它正常工作?

提前致谢!

修改

这是循环:

foreach($whome_answer[weather][forecast_conditions] AS $condition) {

    # KONTROLL
    if(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'mån') {
        $day = 'måndag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tis') {
        $day = 'tisdag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'ons') {
        $day = 'onsdag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tors') {
        $day = 'torsdag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'fre') {
        $day = 'fredag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'lör') {
        $day = 'lördag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'sön') {
        $day = 'söndag';
    }



    echo '<a href="javascript:void(0)" class="forecast-link">';
        echo $day;
    echo '</a>';

    echo (count($condition) -1) <= 4 ? ', ' : ', eller ';

}

以下是我从http://www.google.com/ig/api?weather=,,,59378217,13504219&hl=en

获取内容的链接

编辑(解决方案,但还有一个问题) 下面的代码是解决方案,但它现在打印onsdag, torsdag, fredag, eller lördag,而不是我想要的。如何在“lördag”之后删除最后一个逗号?如果我在“if标记”之后添加$i,则会出现以下情况:onsdag, 2torsdag, 3fredag, eller 4lördag, 5

如何解决此问题?

$i = 1;
foreach($whome_answer[weather][forecast_conditions] AS $condition) {
    $i++;

    # KONTROLL
    if(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'mån') {
        $day = 'måndag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tis') {
        $day = 'tisdag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'ons') {
        $day = 'onsdag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tors') {
        $day = 'torsdag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'fre') {
        $day = 'fredag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'lör') {
        $day = 'lördag';

    # KONTROLL
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'sön') {
        $day = 'söndag';
    }



    echo '<a href="javascript:void(0)" class="forecast-link">';
        echo $day;
    echo '</a>';

    echo $i != 4 ? ', ' : ', eller ';

}

3 个答案:

答案 0 :(得分:3)

在名为$ i的foreach之外实例化一个变量并将其设置为0并为每个循环增加1并检查该变量。

因为它是php我不认为count返回dom对象的位置,它正在计算其他东西,可能是你在$ condition中有多少值。

请考虑这个代码,程序员编程,更容易阅读:)

$i = 0;
foreach($whome_answer[weather][forecast_conditions] AS $condition) {
    $i++;
    switch($condition['day_of_week']['@attributes']['data']) {
        case 'mån':
            $day = 'måndag';
        break;
        case 'tis':
            $day = 'tisdag';
        break;
        case 'ons':
            $day = 'onsdag';
        break;
        case 'tors':
            $day = 'torsdag';
        break;
        case 'fre':
            $day = 'fredag';
        break;
        case 'lör':
            $day = 'lördag';
        break;
        case 'sön':
            $day = 'söndag';
        break;
    }

echo '<a href="javascript:void(0)" class="forecast-link">';
    echo $day;
echo '</a>';

if($i == 4)
    echo ', eller';
 else {
        if($i < 4)
        echo ',';
    }

}

此致 托拜厄斯

答案 1 :(得分:0)

你错过了count()条件语句的回显,这基本上会使整个语句无论count()的结果是什么都没有。如果这是复制/粘贴错误,那么您可以提供更多的源代码吗?

答案 2 :(得分:0)

我正在查看代码...然后在您的打印示例中,我注意到......在每个$条件之后,您正在打印','|',eller'。这会使事情变得复杂,因为您要在检查条件之前打印到屏幕。

逗号对于正确可读的语法至关重要,因此无论如何都要保留它。

另一方面,'eller'可以在打印到屏幕之前进行检查。

所以我对你的代码的解释会更像这样......

$counter = 1;
$amount = count($whome_answer[weather][forecast_conditions]);
foreach($whome_answer[weather][forecast_conditions] AS $condition) {
    if ($counter == 4) echo 'eller ';
    echo $condition['day'];
    if ($counter != $amount) echo ', ';
    $counter++;
}

我假设您可以根据已有的数据确定当天,并为每个$条件构成“日期”的关键。

同样,我注意到你的逻辑(count($condition) -1) <= 4 ? ', ' : ', eller ',如果count($condition)等于5,将始终测试为true,因此只会返回逗号。

编辑:做出更改以回应新问题。