0

我有这个以下数组。在这里,我想计算fraction值为 1 的变量。根据出现次数,我想做一些操作。我知道的一种方法是,通过使用循环数组foreach并放置一个增量器来识别有多少fraction值为 1。但我想知道有没有比上述更简单的方法?

有人可以建议我吗?

Array
(
    [0] => Array
        (
            [answer] => <p>
    Quiz Test Interoperability</p>

            [feedback] => 
            [fraction] => 0
        )

    [1] => Array
        (
            [answer] => <p>
    Quiz Test Interrelationship</p>

            [feedback] => <br />

            [fraction] => 0
        )

    [2] => Array
        (
            [answer] => <p>
    Quiz Team Interoperability</p>

            [feedback] => <br />

            [fraction] => 0
        )

    [3] => Array
        (
            [answer] => <p>
    Queation Test Interoperability</p>

            [feedback] => <br />

            [fraction] => 1
        )

)
4

1 回答 1

4

您可以使用array_reduce()

<?php
function countFractionIs1 ($element) {
   return ($element['fraction'] == 1) ? 1 : 0;
}

$count = array_reduce($yourarray, "countFractionIs1", 0);
于 2013-02-25T11:40:40.260 回答