1

In php,I have one ArrayList. Lets say

$list = {1000,7000,5000,1000,6000,5000,1000,2000};

So what I want to do is that Make count of each element in list:

For example as above ,

      1000 comes three times then count of 1000 = 3

      5000 comes two times then count of 5000 = 2,etc.

And I want to access that count of different elements separately.

Edit:

Here I have for loop

    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)

So How can I do this ?

Please Guide me. Any help will be appreciated.

Ponting
  • 2,248
  • 8
  • 33
  • 61

1 Answers1

3

Try with array_count_values like

$cnt_arr = array_count_values($list);

foreach($cnt_arr as $key => $value) {
    echo "Count of ".$key." = ".$value."<br>";
}

See this LINK

As per your edit,You need to store them like array like

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;
    }
}
GautamD31
  • 28,552
  • 10
  • 64
  • 85