Name Count in foreach loop

7 posts by 3 authors in: Forums > CMS Builder
Last Post: September 13, 2017   (RSS)

By Zicky - September 10, 2017

I'm trying to count the total matching names inside of a foreach loop and produce a number that sits beside it's name. But all I get is a "Notice: Array to string conversion" message.

I need to count to totals for people's matching first names, and list them as seen below in the example. So if there's 24 people named John, the results displayed would read as "John: 24".

Example of what I'm trying to achieve:

John: 24
Tommy: 18
Victor: 12
Jane: 21
Janet: 13

<?php foreach ($namesCountRecords as $record): ?>
$results = array($record['first_name'].",");
$count = array();
    foreach ($results as $data){
       isset($count[$data]) ? $count[$data]++ : $count[$data] = 1;
       echo htmlencode($record['first_name'].":"); echo $count;
<?php endforeach ?>

Anyone have any suggestions?

Thanks, Zicky

By Zicky - September 11, 2017

Thanks to help of the thread below I was able to remove the duplicates from my foreach loop.

But I still can't figure out how to perform the total of each name in counting function.

Example of what I'm trying to achieve:

John: 24
Tommy: 18
Victor: 12
Jane: 21
Janet: 13

Example of what I'm getting, which is only counting a single instance of the first name and not all the Johns, Tommys, Victors, Janes, and Janets:

John: 1
Tommy: 1
Victor: 1
Jane: 1
Janet: 1

<?php $alreadyListed = []; ?>
<?php foreach ($namesCountRecords as $key => $record): ?>
    <?php if(!in_array(strtolower($record['first_name']), $alreadyListed)): ?>
        <?php echo $record['first_name'] ?>
        <?php $alreadyListed[] = strtolower($record['first_name']); ?>
            <?php $str = $record['first_name']; ?>
            <?php $key = count($str); ?>
            <?php print_r($key); ?><br>
    <?php endif; ?>
<?php endforeach ?>

Anyone have any suggestions on how to get this to work or a better way to remove duplicate names and still count the totals for person's similar each name?

Also, any suggestions on how to change <?php print_r($key); ?> to an <?php echo $key; ?> ? When I try this it breaks.

Thanks, Zicky

By Dave - September 11, 2017

Hi Zicky, 

How about this? 

  $firstNames    = array_column($namesCountRecords, 'first_name');
  $namesToCounts = array_count_values($firstNames);
  foreach ($namesToCounts as $firstName => $count) {
    print htmlencode($firstName) .": $count<br/>\n";   

Let me know if that works for you!

Dave Edis - Senior Developer

By Zicky - September 11, 2017

Dave - Thank!!! You're the man!

Works like a charm and it's so clean.

By Deborah - September 13, 2017

Zicky, thanks for sharing this with us all!

I set up a chart according to your example and it works beautifully. If I use Chartjs on a future project, I'll be sure to post back here with my code.

~ Deborah

By Zicky - September 13, 2017

Glad you found it useful. I'll post up any other charts I build as well. Got a few others in mind I plan to build.

Cheers, Zicky