I usually don't like to give up but it feels like this could be more efficient, but for the life of me I can't see it.
I have a Webservice from which I can request the Models, Makes and Types of Cars.
But to be able to request the Types I need the Makes and to be able to request the Makes I need the Models...
Because I need ALL Types (last level) to iterate over them and use them in a different piece of functionality I need to make these 3 steps.
The only problem: my current code takes about 3 minutes to finish and it feels incorrect because of Big O..
If you guys could make this more efficient I would be forever gratefull:
$cars = [];
$makes = getMakes()['makes']['Make'];
$models = [];
$types = [];
$allTypes = [];
$timeStart = microtime(true);
// Takes waaaaaay too long
foreach ($makes as $key => $make) {
$cars[$key]['makeName'] = $make['makename'];
$cars[$key]['makeCode'] = $make['makecode'];
$models = getModels($make['makecode'])['models']['Model'];
foreach ($models as $subKey => $model) {
if (is_array($model)) {
if (array_key_exists('modelname', $model)) {
$cars[$key]['models'][$subKey]['modelName'] = $model['modelname'];
}
if (array_key_exists('modelcode', $model)) {
$cars[$key]['models'][$subKey]['modelCode'] = $model['modelcode'];
}
$types = getTypes($model['modelcode']);
foreach ($types as $ssKey => $type) {
if (is_array($type)) {
if (array_key_exists('typecode', $type)) {
$cars[$key]['models'][$subKey]['types'][$ssKey]['typeCode'] = $type['typecode'];
$allTypes[] = $type['typecode'];
}
}
}
}
}
}
$timeEnd = microtime(true);
print_r(($timeEnd - $timeStart) / 60); // 3.228285531203 (~3 minutes)
// print_r($cars);