在php中,我有一个ArrayList。让我们说
$ list = {1000,7000,5000,1000,6000,5000,1000,2000};
所以我想做的是对列表中的每个元素进行计数:
例如,如上所述,
1000 comes three times then count of 1000 = 3 5000 comes two times then count of 5000 = 2,etc.
我想要access that count of different elements separately
。
修改
这里我有循环
for($i=0;$i<count($trip);$i++)
{
// Here list will be new everytime for loop run.
$list = $Users->Matches($trip[$i]);
}
// ----------------------------------------------------------------------
Here what I want to do is that "Take all the element of list for value of
$i : 0 to count($trip)" and "add it into another list lets say $allList
and access" as below :
// -----------------------------------------------------------------------
$cnt_arr = array_count_values($allList);
foreach($cnt_arr as $key => $value) {
echo "\n\n Count of ".$key." = ".$value." ";
}
输出:
Lets say for loop runs first time :
$list = {1000,7000,5000}
Now for loop second time :
$list = {8000,1000,9000}
Now for loop is completed and at the end I want the $allList value as below :
$allList = {1000,7000,5000,8000,1000,9000}(which has all the elements of above-appended list)
那我怎么能这样做?
请指导我。任何帮助将不胜感激。
答案 0 :(得分:3)
尝试array_count_values
喜欢
$cnt_arr = array_count_values($list);
foreach($cnt_arr as $key => $value) {
echo "Count of ".$key." = ".$value."<br>";
}
请参阅此LINK
根据您的编辑,您需要像
这样的数组存储它们for($i=0;$i<count($trip);$i++)
{
// Here list will be new everytime for loop run.
$temp_list = $Users->Matches($trip[$i]);
foreach($temp_list as $tmp) {
$list[] = $tmp;
}
}