I have a lot of small arrays (see image) that share similar data (Delta Dental). I need to count how many times the same “GroupName” shows up with Delta Dental present.
Current code to grab these arrays is
preg_match('/(Delta Dental)[ ]([a-zA-Z]*[ ]*)+/', $details['Name'])
What’s your question?
Sorry - I need a way to count how many times the same “GroupName” shows up with Delta Dental present, that resets when the GroupName changes.
The end goal is to combine similar data for the same “GroupName”, so the Atlantic GroupName would have a “Name” of “Delta Dental Hawki/PPO/Premier”.
Here’s my full code snippet for this function:
if (in_array($details['Name'], $masterList[$key]) === false && preg_match('/(Delta Dental)[ ]([a-zA-Z]*[ ]*)+/', $details['Name']) === 1) {
if (preg_match("/(State Employee Plan)/", $details['Name'])) {
$dd[][] = "State Employee Plan";
} else if (preg_match("/(Wilson McShane)/", $details['Name'])) {
$dd[][] = "Wilson McShane";
} else {
$dd[][] = trim(strrchr($details['Name'], ' '));
}
if (count($dd) > 4) {
for ($i = 0; $i < count($dd); $i++) {
$merge = array_merge($merge, $dd[$i]);
}
$unique = array_values(array_unique($merge));
$merge = array();
if ($unique[0] === "State Employee Plan") {
$shifted = array_shift($unique);
array_push($unique, $shifted);
}
sort($unique);
array_unshift($unique, "Delta Dental");
$newVal = $unique[0] . ' ' . $unique[1];
for ($j = 2; $j < count($unique); $j++) {
$newVal .= '/' . $unique[$j];
}
if ($newVal) {
array_push($masterList[$key], $newVal);
$newVal = '';
$unique = array();
$dd = array();
}
}
}`
The count($dd) > 4
changes between each GroupName so I need a way to count the GroupName instances before combining the names.
The error is that some names are combining with others, so some results are “Delta Dental PPO/PPO/Premier/Premier”.
Thanks!
Where are these arrays coming from in the first place? If a database, a query will solve your problem.
These are coming from an API and do show as individual arrays.
I think what I am going to do is build an array of offices that have Delta Dental, and run array_count_values
on that array to get my count for each GroupName.
foreach ($detailsList as $details){
if (preg_match('/(Delta Dental)[ ]([a-zA-Z]*[ ]*)+/', $details['Name']) === 1){
$ddOffices[] = $details['GroupName'];
}
}
Is this API available for us to test with?
Unfortunately it is not.
How about an exact parse able array? It looks like it would be what you have for $dd.
$dd would end up something like this:
Array (
[0] => Array
(
[0] => PPO
)
[1] => Array
(
[0] => Premier
)
)
Sometimes $dd would have more than 2 arrays in it, depending on what values come through.
Sorry for my unthorough explanation up to this point. There’s a lot going on with this API/script.
My solution ended up being:
if (preg_match('(Delta Dental)', $details['Name']) === 1) {
$ddOffices[] = $details['GroupName'];
}
//delta dental count list
$ddCountVals = array_count_values($ddOffices);
$dd = array();
<more code>
$ddCount = $ddCountVals[$details['GroupName']];
if (count($dd) >= $ddCount) {
<more code>
}
When I said parse able, I meant the actual array, not the output from print_r
Not sure I follow then? $dd is an empty place-holding array that gets reset after the data is compiled further down.