Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count data from an array based on another array's value in PHP?

I have an array that contains data of incoming mail, looks something like this:

$incomingMail = [
   0 => [
      'title' => 'Title 1',
      'areaCode' => 101
   ],
   1 => [
      'title' => 'Title 2',
      'areaCode' => 101
   ],
   2 => [
      'title' => 'Title 3',
      'areaCode' => 102
   ]
];

And another array containing area name and code, the array looks like this:

$areaArr = [
   0 => [
      'name' => 'Area 1',
      'code' => 101
   ],
   1 => [
      'name' => 'Area 2',
      'code' => 102
   ],
   2 => [
      'name' => 'Area 3',
      'code' => 103
   ],
   3 => [
      'name' => 'Area 4',
      'code' => 104
   ]
];

I want to create an array that contains the count of incomingMail array based on areaArr‘s code, it will kind of looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$areaWithMailCount = [
   0 => [
      'areaName' => 'Area 1',
      'areaCode' => 101,
      'mailCount' => 2
   ],
   1 => [
      'areaName' => 'Area 2',
      'areaCode' => 102,
      'mailCount' => 1
   ],
   2 => [
      'areaName' => 'Area 3',
      'areaCode' => 103,
      'mailCount' => 0
   ],
   3 => [
      'areaName' => 'Area 4',
      'areaCode' => 104,
      'mailCount' => 0
   ]
];

I have tried to loop those arrays and add the condition based on area code but the result isn’t quite what I wanted, the code looks something like this:

$areaWithMailCount = [];

foreach($areaArr as $area) {
   foreach($incomingMail as $mail) {
      if($mail['areaCode'] == $area['code']) {
         $areaWithMailCount[] = [
            'areaName' => $area['name'],
            'areaCode' => $area['code'],
            'mailCount' => count($mail)
         ];
      }
   }
}

The result from above code is like this:

[
  0 => [
    "areaName" => "Area 1"
    "areaCode" => 101
    "mailCount" => 2
  ],
  1 => [
    "areaName" => "Area 1"
    "areaCode" => 101
    "mailCount" => 2
  ],
  2 => [
    "areaName" => "Area 2"
    "areaCode" => 102
    "mailCount" => 2
  ]
];

Any ideas how to achieve that? Thanks in advance.

>Solution :

You can achieve the desired result using a nested loop to iterate over each element of $areaArr, and for each element, loop through $incomingMail to count the number of mails with the same area code. Here’s how you can do it:

$areaWithMailCount = [];

foreach ($areaArr as $area) {
    $mailCount = 0;
    foreach ($incomingMail as $mail) {
        if ($mail['areaCode'] == $area['code']) {
            $mailCount++;
        }
    }
    $areaWithMailCount[] = [
        'areaName' => $area['name'],
        'areaCode' => $area['code'],
        'mailCount' => $mailCount
    ];
}

In this code, we first initialize the mail count to 0. We then loop through $incomingMail and check if the area code matches that of the current $area. If it does, we increment the mail count. Finally, we add a new element to $areaWithMailCount with the area name, code, and mail count.

This should give you the desired result in $areaWithMailCount.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading