我有一个类似下面的数组。它会重复一段时间,但我想跟踪的是“Question_id”最多显示1。
对于每个movie_id,它会询问相同的问题。在数组之前,它显示了其中5个问题。因此,为了更好地解释每个movie_id
的数组,有question_ids
。对于每个movie_id
和question_id
,都有一个0或1.这就是说,这是铁人三,这里是“其中有qwyneth paltrow吗?”然后是“1”。
基本上,我想对这个数组进行排序,并找出1最常见的问题。因此,如果您查看数组,对于question_id => 3
,两部电影的值均等于1.
我想找到所有电影合并的question_id
,其中包含最多的电影。我希望这是有道理的!
Array ( [0] => Array
( [0] => stdClass Object ( [movie_id] => 1 [question_id] => 1 [value] => 0 ) [1] => stdClass Object ( [movie_id] => 1 [question_id] => 2 [value] => 0 ) [2] => stdClass Object ( [movie_id] => 1 [question_id] => 3 [value] => 1 ) [3] => stdClass Object ( [movie_id] => 1 [question_id] => 4 [value] => 0 ) [4] => stdClass Object ( [movie_id] => 1 [question_id] => 5 [value] => 1 ))
( [0] => stdClass Object ( [movie_id] => 2 [question_id] => 1 [value] => 0 ) [1] => stdClass Object ( [movie_id] => 2 [question_id] => 2 [value] => 0 ) [2] => stdClass Object ( [movie_id] => 2 [question_id] => 3 [value] => 1 ) [3] => stdClass Object ( [movie_id] => 2 [question_id] => 4 [value] => 0 ) [4] => stdClass Object ( [movie_id] => 2 [question_id] => 5 [value] => 1 )))
更新:我设置了一个尝试类似这样的路径,上面的数组被称为$ movie_list:
foreach($movie_list] as $v) {
@$counts[$v['movie_id']]+=$v['value'];
}
我真的不知道如何实现它,我甚至不确定它是如何工作的。
修正了这个问题:
for($i =0; $i<count($movie_list);$i++){
foreach($movie_list[$i] as $v) {
$counts[$v->question_id]+=$v->value;
}
}