Last active
June 14, 2018 00:44
-
-
Save joeswann/ecff38ee492e40a8603434bb26fd6e25 to your computer and use it in GitHub Desktop.
[normalise_array.php] Normalise a nested array
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
function formatOptionGroups($raw_option_groups) { | |
//Normalise everything | |
$return = [ | |
'option_groups' => [], | |
'option_types' => [], | |
'option_values' => [], | |
'option_prices' => [], | |
]; | |
//Foreach Group (eg Size/Simple Prong) | |
foreach($raw_option_groups as $raw_option_group) { | |
$base_option_group = $this->mapObject($raw_option_group); | |
$group_option_types = $this->mapObjects($raw_option_group->optionTypes, ['optionValues']); | |
$group_option_values = $this->mapObjects($raw_option_group->optionValues, ['options', 'display']); | |
//Foreach group type (eg Cut) | |
foreach($group_option_types as $group_option_type) { | |
//The applicable option prices in the intersection of the groups values with This types values | |
$option_type_values = $this->mapObjects($group_option_type['optionValues'], ['options', 'display']); | |
$type_option_values = $this->arrayIntersect($group_option_values, $option_type_values); | |
foreach($type_option_values as $type_option_value) { | |
$value_option_prices = $this->mapObjects($type_option_value['options'], ['priceModifier']); | |
foreach($value_option_prices as $value_option_price) { | |
//This option price may already exist | |
$existing_option_price_id = $this->findInArrayByID($return['option_prices'], (int)$value_option_price['id']); | |
if($existing_option_price_id) { | |
//Just add option value id to id list | |
$return['option_prices'][$existing_option_price_id]['option_value_ids'][] = (int)$type_option_value['id']; | |
} else { | |
//Add option price to option price array | |
$value_option_price['option_value_ids'] = [(int)$type_option_value['id']]; | |
$value_option_price['option_group_id'] = (int)$base_option_group['id']; | |
$value_option_price['option_type_id'] = (int)$group_option_type['id']; | |
$return['option_prices'][] = $value_option_price; | |
} | |
} | |
//Add option value to option value array | |
$type_option_value['option_group_id'] = (int)$base_option_group['id']; | |
$type_option_value['option_type_id'] = (int)$group_option_type['id']; | |
$return['option_values'][] = $type_option_value; | |
} | |
//Add option type to option type array | |
$group_option_type['group_id'] = (int)$base_option_group['id']; //Add option group id to option type | |
$return['option_types'][] = $group_option_type; | |
} | |
//Add option group to option groups | |
$return['option_groups'][] = $base_option_group; | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment