2013-08-31 27 views
1

在PHP中,我有一个ArrayList中。比方说如何计算在阵列多于一个(PHP)的元素

$名单= {} 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." "; 
    } 

OUTPUT:

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) 

所以,我怎么能这样做?

请引导我。任何帮助将不胜感激。

回答

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; 
    } 
} 
+0

感谢answer.It工程确定为array_count_values($列表)。但是对于echo“Count of”。$ key。“=”。$ value。“
”;在打印这样的计数0 = 1000
Ponting

+0

对我来说是正确的来了... – Gautam3164

+0

认为我们需要'$ cnt_arr' – Gautam3164