function getParent(&$categories, $category){
foreach ($categories as &$cat){
if($category->parentId==$cat[0]->id){
$cat["subCategories"][$category->id] = array($category,"subCategories"=>array());
return $cat;
}
else if(isset($cat["subCategories"])){
$this->getParent($cat["subCategories"], $category);
}
}
}
$categories(它是一个 chanined 列表,包含一个自我 id 和一个外国 id(是一个列表,$category是类别的一个元素。我不知道这句话是什么意思$category->parentId==$cat[0]->id
为什么是 [0]?为什么总是 0?你能解释整个代码吗?
if 正在检查当前类别 Id 是否与父 id 相同,然后返回一个数组,否则它会再次运行以查找下一个父级。
$category->parentId == $cat[0]->id
[0] 是常量,因为它指的是数组的第一项,所以不是知道数组键,而是简写。
php 中的数组可以有一个键 $cat['current_id'],如果这是第一个键,它也将是 $cat[0],给出相同的结果。
$array = Array('current_id' => 55, 'title' => 'first one');
echo $array['current_id'];
echo $array[0];
if ($array['current_id'] == $array[0]){ echo 'Same'; } else { echo 'Not Same'; }
if ($array['current_id'] == $array[1]){ echo 'Same'; } else { echo 'Not Same'; }
对于键 0,1,2,3,所有数组都从 0 开始...
http://php.net/manual/en/language.types.array.php#example-99
http://oreilly.com/catalog/progphp/chapter/ch05.html