API返回类似的类别:
[1] => Array
(
[category_id] => 14308
[parent_id] => 14308
[level] => 0
)
[2] => Array
(
[category_id] => 179836
[parent_id] => 14308
[level] => 1
)
[3] => Array
(
[category_id] => 230022
[parent_id] => 179836
[level] => 2
)
我需要将它们插入到DB中,如下所示:
╔═════════════╦═══════════╗
║ category_id ║ parent_id ║
╠═════════════╬═══════════╣
║ 14308 ║ 0 ║
║ 179836 ║ 14308 ║
║ 230022 ║ 179836 ║
╚═════════════╩═══════════╝
…这很容易,但是category_to_path表很棘手,我似乎无法弄明白.它必须是这样的:
╔═════════════╦═════════╦═══════╗
║ category_id ║ path_id ║ level ║
╠═════════════╬═════════╬═══════╣
║ 14308 ║ 14308 ║ 0 ║
║ 179836 ║ 14308 ║ 0 ║
║ 179836 ║ 179836 ║ 1 ║
║ 230022 ║ 14308 ║ 0 ║
║ 230022 ║ 179836 ║ 1 ║
║ 230022 ║ 230022 ║ 2 ║
╚═════════════╩═════════╩═══════╝
这是我到目前为止:
$path_ids;
for ($i=0; $i <= ($category->CategoryLevel-1); $i++) {
// -1 cause the API returns a non zero-based level
$path_ids[$i]['category_id'] = $category->CategoryID;
if ($category->CategoryLevel-1 == $i) {
$path_ids[$i]['path_id'] = $category->CategoryParentID[0];
}
elseif ($category->CategoryLevel-1) {
// ?
}
$path_ids[$i]['level'] = $i;
}