Last active
June 15, 2018 16:23
-
-
Save Ma-ve/253c9b1fd9252f764c7ceee20c52f13f to your computer and use it in GitHub Desktop.
DossierItem LinkedTo based on list of array of external ids
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 | |
/** | |
* @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