0

Maybe I am looking past something, early morning.

I have an array of 'prescriptions' that I get from my database and I store it in a public variable in my class. A quick example of what that array looks like after the SQL call...

$prescriptions = [
    0 => [
        'companyName' => 'FoorBar Inc.',
        'prescription' => 'SomePrescription',
        'cost' => '$9.98',
    ],
    1 => [
        'companyName' => 'FoorBar Inc.',
        'prescription' => 'SomeOtherPrescription',
        'cost' => '$15.98',
    ],
    2 => [
        'companyName' => 'Labs Inc.',
        'prescription' => 'SomePrescription',
        'cost' => '$12.38',
    ],
    3 => [
        'companyName' => 'Lorem Ipsum Inc.',
        'prescription' => 'SomePrescription',
        'cost' => '$100.98',
    ],
    4 => [
        'companyName' => 'Lorem Ipsum Inc.',
        'prescription' => 'SomePrescription',
        'cost' => '$53.08',
    ],
];

Now what I am looking to do is group these prescriptions by company but still persist all data attached to the prescription, so I have the following logic:

public function groupByCompany()
{
    $clinicArray = [];

    foreach ($this->prescriptions as $prescription) {
        $clinicArray[$prescription["companyName"]] = [];
    }

    foreach ($this->prescriptions as $prescription) {
        $clinicArray[$prescription["companyName"]][] = $prescription;
    }

    return $clinicArray;
}

This works just fine, I get my desired result. I just feel like there is a better way to do this and maybe without running a foreach loop twice. Any suggestions?

Thank You Everyone.

localheinz
  • 9,179
  • 2
  • 33
  • 44
Taylor Foster
  • 1,103
  • 1
  • 11
  • 24
  • 3
    I don't see anything wrong with putting both lines in the first loop. – Bluebaron Aug 11 '17 at 13:08
  • `Any suggestions` .I'd start with "don't fix until it's broken". You are trying to optimize thing you are not even sure is not-optimal. Does it really makes sense in that order? – Marcin Orlowski Aug 11 '17 at 13:08
  • @MarcinOrlowski I guess part of refactoring code is "fix if it can be better even if it's not broken". Is it not so? – Tom Aug 11 '17 at 13:14
  • Sure, but first you need to know that it is bad, ineffective or faulty in that way or another. Having nested foreach does not automatically make your code such – Marcin Orlowski Aug 11 '17 at 13:15

2 Answers2

2

With one foreach it is:

public function groupByCompany()
{
    $clinicArray = [];

    foreach ($this->prescriptions as $prescription) {
        $companyName = $prescription["companyName"];

        if (!isset($clinicArray[$companyName])) {
            $clinicArray[$companyName] = [];
        }

        $clinicArray[$companyName][] = $prescription;

    }

    return $clinicArray;
}
localheinz
  • 9,179
  • 2
  • 33
  • 44
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

There are many ways to change the logic in your code to group the loops.

The most common in PHP is to check if the item of the array already exists using the isset() function:

foreach ($this->prescriptions as $prescription) :

    if (!isset($clinicArray[$prescription["companyName"]])) {
        $clinicArray[$prescription["companyName"]] = [];
    }
    $clinicArray[$prescription["companyName"]][] = $prescription;

endforeach;
Gustavo Jantsch
  • 382
  • 2
  • 9