Jump to content

Recommended Posts

I have an array of first and last names joined by an underscore (eg: Sally_Smith).

Ultimately, I want to count the number of people (elements) with the same last name.

My thinking is to run through the array and explode each element to isolate the last names, then use array_unique to create an array of $lastNameOnly and eventually use this as a counting mechanism against the original array..

Am I over-complicating this?

Link to comment
https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/
Share on other sites

if all you want is a COUNT() per last name, you can do that in the query that's getting the data, with a GROUP BY last_name term.

if you want the name data and a count for each last name, i would index/pivot the data using the last name as the main array index when you fetch the data, from wherever it is coming from.

  On 4/9/2025 at 4:30 AM, mac_gyver said:

if all you want is a COUNT() per last name, you can do that in the query that's getting the data, with a GROUP BY last_name term.

if you want the name data and a count for each last name, i would index/pivot the data using the last name as the main array index when you fetch the data, from wherever it is coming from.

Expand  

From the way the question was worded, it doesn't appear that the data is in a relational database.

  On 4/9/2025 at 4:30 AM, mac_gyver said:

if you want the name data and a count for each last name, i would index/pivot the data using the last name as the main array index when you -

Expand  

 

  On 4/9/2025 at 5:03 AM, phppup said:

generated (it) from other PHP coding.

Expand  

if you post the code generating the array, someone could post an example, instead of just writing about how to do it.

Edited by mac_gyver
  On 4/9/2025 at 3:59 AM, phppup said:

I have an array of first and last names joined by an underscore (eg: Sally_Smith).

Ultimately, I want to count the number of people (elements) with the same last name.

My thinking is to run through the array and explode each element to isolate the last names, then use array_unique to create an array of $lastNameOnly and eventually use this as a counting mechanism against the original array..

Am I over-complicating this?

Expand  

To a degree, because you can create your desired list/count in one pass through the array.  

<?php

$names = ['bob_jones', 'sam_smith', 'jane_doe', 'john_smith', 'jill_jackson', 'matt_jones', 'john_doe', 'emily_smith'];
$lastNames = [];

foreach ($names as $name) {
    $lname = substr($name, strpos($name, '_') +1);
    if (array_key_exists($lname, $lastNames)) {
        $lastNames[$lname]++;
    } else {
        $lastNames[$lname] = 1;
    }
}

var_dump($lastNames);

 

Hopefully, it's clear to you that Barand's solution and the one I presented are the same, only Barand used a 100% functional approach, and a lambda (anonymous function) passed to array_map.  

Compare/contrast the two solutions, and if you can understand them both, you'll have a good basis for solving future problems like this.

One thing I would note about these solutions, is that any solution is only as good as its suitability to the data.  For example, if the names can have suffixes like jr, sr, 3rd etc, you'll probably not get what you want.  They could be improved to handle those situations, should the data you're working with warrant it.

Using PHPUnit to create unit tests for code like this is a really valuable investment, if you care about testing, maintainability and quality.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.