Как избавиться от повторение кода php
Как избавиться от повторения кода?
if(!empty($yml_category[strval($row->categoryId)])){
$offer_category = $yml_category[strval($row->categoryId)]['name']." | ";
$parentId = $yml_category[strval($row->categoryId)]['parentId'];
if($parentId !== 0){
$offer_category = $offer_category.$yml_category[$parentId]['name']." | ";
$parentId = $yml_category[$parentId]['parentId'];
if($parentId !== 0){
$offer_category = $offer_category.$yml_category[$parentId]['name']." | ";
$parentId = $yml_category[$parentId]['parentId'];
if($parentId !== 0){
$offer_category = $offer_category.$yml_category[$parentId]['name']." | ";
$parentId = $yml_category[$parentId]['parentId'];
if($parentId !== 0){
$offer_category = $offer_category.$yml_category[$parentId]['name']." | ";
$parentId = $yml_category[$parentId]['parentId'];
if($parentId !== 0){
$offer_category = $offer_category.$yml_category[$parentId]['name']." | ";
}
}
}
}
}
}
Ответы (2 шт):
Автор решения: Anton Shchyrov
→ Ссылка
Как обычно - использовать циклы
$category = strval($row->categoryId);
if(!empty($yml_category[$category])) {
$offer_category = "";
do {
$offer_category .= $yml_category[$category]['name'] . " | ";
$category = $yml_category[$category]['parentId'];
} while ($category !== 0);
}
Автор решения: vp_arth
→ Ссылка
$offer_category = '';
do {
$offer_category .= $yml_category[strval($row->categoryId)]['name']." | ";
$parentId = $yml_category[strval($row->categoryId)]['parentId'];
while ($parentId !== 0);