Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @param array $ids
- *
- * @return array
- */
- private function getLinkedToInfoByArrayOfIds($ids) {
- if(empty($ids)) {
- return [];
- }
- $group = [
- 'person' => 'relation',
- 'organization' => 'relation',
- 'project' => 'project',
- 'sales' => 'sales',
- ];
- $linkedTo = [
- 'person' => 'person_id',
- 'organization' => 'organization_id',
- 'project' => 'project_id',
- 'sales' => 'sales_id',
- ];
- // Group them by their matched grouping. [ 'person:abc', 'org:abc' => ['relation' => ['person:abc', 'org:abc'] ]
- foreach($ids as $externalId) {
- $explode = explode(':', $externalId);
- if(!isset($groupedData[$group[$explode[0]]])) {
- $groupedData[$group[$explode[0]]] = [];
- }
- $groupedData[$group[$explode[0]]][] = $externalId;
- }
- // Get the highest count of groupedData (['relation' => ['person:abc', 'org:abc'], 'project' => ['project:abc']] returns 2
- $max = count(max($groupedData));
- // Loop the maximum times of occurences, then assign items to each array, and unset them for the next loop
- $return = [];
- for($i = 0; $i < $max; $i++) {
- foreach($groupedData as $type => &$items) {
- if(isset($items[$i])) {
- $explode = explode(':', $items[$i]);
- $key = $linkedTo[$explode[0]]; // Resolve person:abc to ['person_id' => 'person:abc']
- $return[$i][$key] = $items[$i];
- unset($items[$i]);
- }
- }
- }
- return $return;
- }
Add Comment
Please, Sign In to add comment