Guest User

Untitled

a guest
Jun 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. <?php
  2. /**
  3. * @param array $ids
  4. *
  5. * @return array
  6. */
  7. private function getLinkedToInfoByArrayOfIds($ids) {
  8. if(empty($ids)) {
  9. return [];
  10. }
  11. $group = [
  12. 'person' => 'relation',
  13. 'organization' => 'relation',
  14. 'project' => 'project',
  15. 'sales' => 'sales',
  16. ];
  17.  
  18. $linkedTo = [
  19. 'person' => 'person_id',
  20. 'organization' => 'organization_id',
  21. 'project' => 'project_id',
  22. 'sales' => 'sales_id',
  23. ];
  24.  
  25. // Group them by their matched grouping. [ 'person:abc', 'org:abc' => ['relation' => ['person:abc', 'org:abc'] ]
  26. foreach($ids as $externalId) {
  27. $explode = explode(':', $externalId);
  28. if(!isset($groupedData[$group[$explode[0]]])) {
  29. $groupedData[$group[$explode[0]]] = [];
  30. }
  31. $groupedData[$group[$explode[0]]][] = $externalId;
  32. }
  33.  
  34. // Get the highest count of groupedData (['relation' => ['person:abc', 'org:abc'], 'project' => ['project:abc']] returns 2
  35. $max = count(max($groupedData));
  36.  
  37. // Loop the maximum times of occurences, then assign items to each array, and unset them for the next loop
  38. $return = [];
  39. for($i = 0; $i < $max; $i++) {
  40. foreach($groupedData as $type => &$items) {
  41. if(isset($items[$i])) {
  42. $explode = explode(':', $items[$i]);
  43. $key = $linkedTo[$explode[0]]; // Resolve person:abc to ['person_id' => 'person:abc']
  44. $return[$i][$key] = $items[$i];
  45. unset($items[$i]);
  46. }
  47. }
  48. }
  49.  
  50. return $return;
  51. }
Add Comment
Please, Sign In to add comment