Last active
October 13, 2015 01:18
-
-
Save dotJoel/4116675 to your computer and use it in GitHub Desktop.
Merge items in an array based on a subkey, such as quantity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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