Skip to content

Instantly share code, notes, and snippets.

@amine-y
Created November 13, 2023 19:47
Show Gist options
  • Select an option

  • Save amine-y/d66e7cb2a1571a451f82493f447a5742 to your computer and use it in GitHub Desktop.

Select an option

Save amine-y/d66e7cb2a1571a451f82493f447a5742 to your computer and use it in GitHub Desktop.
<?php
$categories = $this->container->get('mz.category')->getMenzzoMenu(3);
function generateCategoryPath($categories, $categoryId) {
$categoryPath = [];
// Find the category with the given ID
$category = findCategoryById($categories, $categoryId);
// If the category is found, traverse its ancestors
if ($category) {
while ($category) {
array_unshift($categoryPath, $category['categoryName']); // Add category to the beginning of the array
$parentId = $category['parentId'];
$category = findCategoryById($categories, $parentId);
}
// Implode the array to create the final path
return implode(' > ', $categoryPath);
}
return "Category not found";
}
function findCategoryById($categories, $categoryId) {
foreach ($categories as $category) {
if ($category['id'] == $categoryId) {
return $category;
}
// Check if the category has children and recursively search in them
if (!empty($category['children'])) {
$childResult = findCategoryById($category['children'], $categoryId);
if ($childResult) {
return $childResult;
}
}
}
return null;
}
$categoryId = 237; // Replace with the desired category ID
$categoryPath = generateCategoryPath($categories, $categoryId);
echo $categoryPath;
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment