I am currently working on a Symfony 6.1 project where I am trying to build a fairly large array in PHP 8.1. Now the principle basically works and the structure is correct. However, there are problems regarding the execution time and the required memory if the number for exceutions for each foreach loop gets higher.
In my current code, that you can see down below, I have also state the number of values for each foreach loop as a comment next to it.
My current code:
$data = array();
foreach($variantInformations as $key => $value) { //Between 1-7 values
foreach($value["sizes"] as $key2 => $value2) { //Between 1-7 values
foreach($value["colors"] as $key3 => $value3) {
if(isset($value3["printColors"])) { //Between 1*88 - 8*88 values
foreach($value3["printColors"] as $key4 => $value4) {
if(isset($value["printMotifs"])) {
foreach($value["printMotifs"] as $key5 => $value5) { //Between 100-300 values
if($value5["colorLevel"] == "1") {
if(!isset($value4["secondPrintColorId"])) {
$data[$key][$key2][$key3][$key4][$key5]["number"] = $variantInformations[$key]["number"]."-".$value2."-".$value3["bodyColor"]["id"]."-".$value4["id"]."-".$value5["id"];
$data[$key][$key2][$key3][$key4][$key5]["title"] = $variantInformations[$key]["title"]."-".$value2."-".$value3["bodyColor"]["title"]."-".$value4["title"]."-".$value5["title"];
$data[$key][$key2][$key3][$key4][$key5]["bodyColor"] = $value3["bodyColor"]["id"];
$data[$key][$key2][$key3][$key4][$key5]["printColor"] = $value4["id"];
$data[$key][$key2][$key3][$key4][$key5]["printMotif"] = $value5["id"];
$data[$key][$key2][$key3][$key4][$key5]["article"] = $this->articleRepository->findOneBy(["number" => $variantInformations[$key]["number"]]);
$data[$key][$key2][$key3][$key4][$key5]["active"] = 0;
$data[$key][$key2][$key3][$key4][$key5]["size"] = $value2;
$data[$key][$key2][$key3][$key4][$key5]["stock"] = "no";
$data[$key][$key2][$key3][$key4][$key5]["assortment"][] = $value["assortment"][0];
$data[$key][$key2][$key3][$key4][$key5]["printMotifStatus"] = $value5["status"];
}
}
elseif ($value5["colorLevel"] == "2") {
if(isset($value4["secondPrintColorId"])) {
$data[$key][$key2][$key3][$key4][$key5]["number"] = $variantInformations[$key]["number"]."-".$value2."-".$value3["bodyColor"]["id"]."-".$value4["id"]."-".$value4["secondPrintColorId"]."-".$value5["id"];
$data[$key][$key2][$key3][$key4][$key5]["title"] = $variantInformations[$key]["title"]."-".$value2."-".$value3["bodyColor"]["title"]."-".$value4["title"]."-".$value4["secondPrintColorTitle"]."-".$value5["title"];
$data[$key][$key2][$key3][$key4][$key5]["bodyColor"] = $value3["bodyColor"]["id"];
$data[$key][$key2][$key3][$key4][$key5]["printColor"] = $value4["id"];
$data[$key][$key2][$key3][$key4][$key5]["secondPrintColorId"] = $value4["secondPrintColorId"];
$data[$key][$key2][$key3][$key4][$key5]["printMotif"] = $value5["id"];
$data[$key][$key2][$key3][$key4][$key5]["article"] = $this->articleRepository->findOneBy(["number" => $variantInformations[$key]["number"]]);
$data[$key][$key2][$key3][$key4][$key5]["active"] = 0;
$data[$key][$key2][$key3][$key4][$key5]["size"] = $value2;
$data[$key][$key2][$key3][$key4][$key5]["stock"] = "no";
$data[$key][$key2][$key3][$key4][$key5]["assortment"][] = $value["assortment"][0];
$data[$key][$key2][$key3][$key4][$key5]["printMotifStatus"] = $value5["status"];
}
}
}
}
}
}
}
}
}
Currently I am thinking about how I can make this process faster and how I can optimize it or if it needs a completely different approach.
Does anyone have any ideas or suggestions?
CodePudding user response:
You can reduce the number of accesses.
if (!isset($value4["secondPrintColorId"])) {
$data[$key][$key2][$key3][$key4][$key5] = [
'number' => $variantInformations[$key]["number"] . "-" . $value2 . "-" . $value3["bodyColor"]["id"] . "-" . $value4["id"] . "-" . $value5["id"],
'title' => $variantInformations[$key]["title"] . "-" . $value2 . "-" . $value3["bodyColor"]["title"] . "-" . $value4["title"] . "-" . $value5["title"],
'bodyColor' => $value3["bodyColor"]["id"],
'printColor' => $value4["id"],
'printModel' => $value5["id"],
'article' => $this->articleRepository->findOneBy(["number" => $variantInformations[$key]["number"]]),
'active' => 0,
'size' => $value2,
'stock' => 'no',
'assortment' => [$value["assortment"][0]],
'printMotifStatus' => $value5["status"],
];
}
Also you are calling the DB with findOneBy()
in the loop. This is slow. You could either cache them when used multiple times or do one query instead of hundreds/thousands.