0

I have the following array and am trying to loop through it with php, generate a list of the "Type" and count how many. The Type is the key, but there will be unique values such as Call, To-do, Meeting, Proposal, etc. My goal is to have the following out put:

Call 2
To-do 1
Meeting 3
Proposal 4

The above are not the values that will be output from the following array, but I wanted you to have an idea of what I am trying to accomplish. Please help!

Array (
    [0] => Array (
        [0] => Call
        [Type] => Call
        [1] => [email protected]
        [EmailAddress] => [email protected]
        [2] => 3xxxx00
        [Phone] => 31xxxx00
        [3] => 31xxxx871
        [MobilePhone] => 31xxxx871
        [4] => 102795
        [CustomerID] => 102795
        [5] => Nortxxxxal
        [Company] => Noxxxxal
        [6] => Frank
        [FirstName] => Frank
        [7] => Syxxxxer
        [LastName] => Sxxxxter
        [8] => 3
        [Priority] => 3
        [9] => invite to Haxxxxales for lunch
        [Details] => invite to Hafxxxxales for lunch
        [10] => 4503
        [ActivityID] => 4503
        [11] => 05/23/13
        [DueDate] => 05/23/13
    )
    [1] => Array (
        [0] => To-do
        [Type] => To-do
        [1] => [email protected]
        [EmailAddress] => [email protected]
        [2] => 315xxxx000
        [Phone] => 3154xxxx0
        [3] => 315xxxx1
        [MobilePhone] => 315xxxx1
        [4] => 102795
        [CustomerID] => 102795
        [5] => Norxxxxl
        [Company] => Norxxxxcal
        [6] => Frxxxxk
        [FirstName] => Fxxxxk
        [7] => Sxxxxr
        [LastName] => Syxxxxer
        [8] => 3
        [Priority] => 3
        [9] => find out who contact is for xxxxdical center
        [Details] => find out who contact is foxxxxcal center
        [10] => 4504
        [ActivityID] => 4504
        [11] => 05/23/13
        [DueDate] => 05/23/13
    )
)
1
  • What do the example loops look like? Commented May 22, 2013 at 23:17

6 Answers 6

3

This should do it:

$type_counts = array_count_values(array_map(function($x) {return $x['Type'];}, $array));

The array_map will return an array containing all the Type elements, then array_count_values will count the number of each of these and return this as an associative array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the responses I am still having no luck getting this to work. I am sure it is some little thing I am missing. Moreover, here is how I am generating my array. When I try and plug the generated array in to the code nothing happens, but using the print_r command outputs the array. <? while($row = mysql_fetch_array($c_result)) { $test[] = $row; } print_r($test); ?>
I just tested my code and it worked. You could also just count the values in your while loop, using code similar to the other answers.
1

something along the lines of:

foreach ($array as $key => $value) {
 if (isset($result[$key])) {
  $result[$key]++;
 } else {
 $result[$key] = 1;
 }
}

if it is a muldi-dimensional array, just put another for each in there but that should give you an idea

2 Comments

Thank you for the responses I am still having no luck getting this to work. I am sure it is some little thing I am missing. Moreover, here is how I am generating my array. When I try and plug the generated array in to the code nothing happens, but using the print_r command outputs the array. <? while($row = mysql_fetch_array($c_result)) { $test[] = $row; } print_r($test); ?>
can you give me part of the array to examine? shouldn't be a big problem
0
$types = array();
foreach($array as $item) {
    isset(${$item['Type']}) ? ${$item['Type']}++ : ${$item['Type']} = 1;
    if(!in_array($item['Type'], $types) {
        $types[] = $item['Type'];
}
foreach($types as $type) {
    echo "$type ${$type}\n";
}

1 Comment

Thank you for the responses I am still having no luck getting this to work. I am sure it is some little thing I am missing. Moreover, here is how I am generating my array. When I try and plug the generated array in to the code nothing happens, but using the print_r command outputs the array. <? while($row = mysql_fetch_array($c_result)) { $test[] = $row; } print_r($test); ?>
0

I think you can loop in your array and count each occorrence

$Call = 0;
$To_do = 0;
$Meeting = 0;
$Proposal = 0;
foreach($array as $data)
{
    if($data['Type'] == 'Call')
    {
        $Call++;
    } else if($data['Type'] == 'To-do')
    {
        $To_do++;
    } else if($data['Type'] == 'Meeting')
    {
        $Meeting++;
    } else if($data['Type'] == 'Proposal')
    {
        $Proposal++;
    }
}

In this way you will have stored in each variable $Call $To_do $Meeting $Proposal its relative count

2 Comments

Thank you for the responses I am still having no luck getting this to work. I am sure it is some little thing I am missing. Moreover, here is how I am generating my array. When I try and plug the generated array in to the code nothing happens, but using the print_r command outputs the array. <? while($row = mysql_fetch_array($c_result)) { $test[] = $row; } print_r($test); ?>
@m1e1b1 did you try to place foreach($test as $data) instead of foreach($array as $data)?
0

you can use (array_count_values($array)); http://php.net/manual/en/function.array-count-values.php This gives count of all the keys in an array.

If you want only for the specific keys then use foreach loop

The one Petros mentioned is the best way to do it.

1 Comment

It's a 2-D array, array_count_values() won't work by itself. You need to use something like my answer.
0

I would do something along the lines of:

$your_array = /* Your Array */
$types_count = array();
foreach ($your_array as $type_array){
    $types_count[$type_array['Type']]++;
}

1 Comment

Thank you for the responses I am still having no luck getting this to work. I am sure it is some little thing I am missing. Moreover, here is how I am generating my array. When I try and plug the generated array in to the code nothing happens, but using the print_r command outputs the array. <? while($row = mysql_fetch_array($c_result)) { $test[] = $row; } print_r($test); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.