我有下面的数组。在这里,我想要对值为1的变量fraction进行计数。根据no.of.occurances,我想做一些操作。我知道的一种方法是,通过使用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
)
)发布于 2013-02-25 19:40:40
您可以使用array_reduce()
<?php
function countFractionIs1 ($element) {
return ($element['fraction'] == 1) ? 1 : 0;
}
$count = array_reduce($yourarray, "countFractionIs1", 0);https://stackoverflow.com/questions/15066090
复制相似问题