Skip to content

Instantly share code, notes, and snippets.

@dotJoel
Last active October 13, 2015 01:18
Show Gist options
  • Save dotJoel/4116675 to your computer and use it in GitHub Desktop.
Save dotJoel/4116675 to your computer and use it in GitHub Desktop.
Merge items in an array based on a subkey, such as quantity
<?php
foreach ($items as $item) {
if (!isset($mergedItems['key']) {
$mergedItems[$item['key']] = $item;
} else {
$mergedItems[$item['key']]['subKey'] += $item['subKey'];
}
}
// reset the array keys to 0, 1, 2, etc..
$mergedItems = array_values($mergedItems);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment