PHP仅显示大于0的Array中的值

时间:2011-07-17 19:27:08

标签: php

我有一系列选择下拉列表:

<select name="ultimate[<?php echo $theme['id']; ?>]">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

<select name="ultimate[<?php echo $theme['id']; ?>]">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>
......
......
......

当我用以下内容打印这些结果时:

print_r($_POST);

我得到了这个:

[ultimate] => Array ( [1] => 3 [2] => 0 [3] => 0 [4] => 3 [5] => 0 [6] => 0 [7] => 0 [8] => 2 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 [29] => 0 [30] => 0 [31] => 0 [32] => 0 [33] => 0 [34] => 0 [35] => 0 [36] => 0 [37] => 0 )

我如何只获取每个id大于0的值?

2 个答案:

答案 0 :(得分:3)

这可能会做你想做的事情

foreach ($ultimate as $key => $var)
{
    if ($var > 0) echo $key.":".$var;
}

答案 1 :(得分:1)

$ultimate = array_filter($ultimate);