Skip to content

Instantly share code, notes, and snippets.

@Ma-ve
Last active June 15, 2018 16:23
Show Gist options
  • Save Ma-ve/253c9b1fd9252f764c7ceee20c52f13f to your computer and use it in GitHub Desktop.
Save Ma-ve/253c9b1fd9252f764c7ceee20c52f13f to your computer and use it in GitHub Desktop.
DossierItem LinkedTo based on list of array of external ids
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment