2 回答
TA贡献1865条经验 获得超7个赞
根据我对您的函数的阅读,您的(示例)数据存在问题。
该parentId和index是在一些项目相同。这将根据我从问题中得出的结论创建一个无限循环。
更好的结构类似于以下内容,并在循环中进行一些错误检查:
function getFullCategoryName($strCategoryId, $arrCategories) {
// set a base / default value
$arrCategoriesNames = [];
// do we even have anything to work with?
if (isset($arrCategories[$strCategoryId])) {
// at least one entry
do {
// get the title
$arrCategoriesNames[] = $arrCategories[$strCategoryId]['title'];
// get the next id and error check the data
if ((isset($arrCategories[$strCategoryId]['parentId'])) &&
($strCategoryId != $arrCategories[$strCategoryId]['parentId'])) {
// next index found and not the same
$strCategoryId = $arrCategories[$strCategoryId]['parentId'];
} else {
// either no parentId or a parentId that matches the current
// index. If that is the case, go no further.
$strCategoryId = false;
}
// you could add another error check if you like.
// if (count($arrCategoriesNames) == count($arrCategories)) {
// // go no further as data has a loop
// $strCategoryId = false;
// }
} while($strCategoryId);
// sort the data? why?
krsort($arrCategoriesNames);
}
// return a string
return implode(' > ', $arrCategoriesNames);
}
并测试您的样本数组;
$result = getFullCategoryName(193450,$arrCategories);
var_dump($result);
返回以下内容:
string(19) "Blood glucose meter"
TA贡献1795条经验 获得超7个赞
该while (is_array($arrCategoryCurr))循环永远不会结束的else块$arrCategoryCurr = NULL;永远不会被调用。
发生这种情况是因为您有一个循环,其中节点 id与其父 id 相同。看看你的数组:
....
'id' => '193450',
'parentId' => '193450',
...
要修复它,请将if语句修改为:
if ($arrCategoryCurr['parentId'] && $arrCategoryCurr['parentId'] != $arrCategoryCurr['id'] && isset($arrCategories[$arrCategoryCurr['parentId']])) {
- 2 回答
- 0 关注
- 147 浏览
添加回答
举报