phppup Posted April 9 Share Posted April 9 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? Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/ Share on other sites More sharing options...
mac_gyver Posted April 9 Share Posted April 9 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. Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652898 Share on other sites More sharing options...
gizmola Posted April 9 Share Posted April 9 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. Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652900 Share on other sites More sharing options...
phppup Posted April 9 Author Share Posted April 9 Correct. No database involved. The array is generated from other PHP coding. Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652901 Share on other sites More sharing options...
mac_gyver Posted April 9 Share Posted April 9 (edited) 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 April 9 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652902 Share on other sites More sharing options...
gizmola Posted April 9 Share Posted April 9 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); Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652903 Share on other sites More sharing options...
Barand Posted April 9 Share Posted April 9 $surname_counts = array_count_values(array_map(fn($v)=>substr($v, strpos($v, '_', 0)+1), $names)); -> Array ( [jones] => 2 [smith] => 3 [doe] => 2 [jackson] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652908 Share on other sites More sharing options...
gizmola Posted April 9 Share Posted April 9 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. Quote Link to comment https://forums.phpfreaks.com/topic/327315-grouping-in-array-or-a-better-way/#findComment-1652927 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.