<?php
// The hierarcical structure of the categories is:
//
// Domain
// - Category
// - - Subcategory
class simulating_categories_object{
var $id;
var $parent;
var $slug;
function __construct($id,$parent,$slug){
$this->id = $id;
$this->parent = $parent;
$this->slug = $slug;
}
}
global $catobj;
$catobj = [];
$catobj[] = new simulating_categories_object(1,0,'domain1');
$catobj[] = new simulating_categories_object(2,0,'domain2');
$catobj[] = new simulating_categories_object(3,0,'domain3');
$catobj[] = new simulating_categories_object(4,1,'category1');
$catobj[] = new simulating_categories_object(5,1,'category2');
$catobj[] = new simulating_categories_object(6,1,'category3');
$catobj[] = new simulating_categories_object(7,4,'subcategory1');
$catobj[] = new simulating_categories_object(8,4,'subcategory2');
$catobj[] = new simulating_categories_object(9,2,'category555');
$catobj[] = new simulating_categories_object(10,9,'subcategory666');
$catobj[] = new simulating_categories_object(11,3,'category777');
function get_the_parent_id_of_a_given_element_id($id){
global $catobj;
foreach($catobj as $category){
if($category->id == $id)
return $category->parent;
}
}
echo "<pre>";
//print_r($catobj);
echo "</pre>";
foreach($catobj as $element)
{
if($element->parent == 0){
// our $element is a domain.
echo "This element is a domain. The slug of the element is ".$element->slug."<br>";
continue;
}
$element_parent_id = get_the_parent_id_of_a_given_element_id($element->parent);
if($element_parent_id == 0){
// our $element is a category
echo "This element is a category. The slug of the element is ".$element->slug."<br>";
continue;
}
$element_parent_parent_id = get_the_parent_id_of_a_given_element_id($element_parent_id);
if($element_parent_parent_id == 0){
// our $element is a subcategory
echo "This element is a subcategory. The slug of the element is ".$element->slug."<br>";
}
}
$result = ['domain1' =>
[ 'category1' => ['subcategory1','subcategory2'],
'category2' => [],
'category3' => []
],
'domain2' =>
[
'category555' => ['subcategory666']
],
'domain3' => [
'category777' => []
]
];
echo "<pre>";
print_r($result);
echo "</pre>";
<pre></pre>This element is a domain. The slug of the element is domain1<br>This element is a domain. The slug of the element is domain2<br>This element is a domain. The slug of the element is domain3<br>This element is a category. The slug of the element is category1<br>This element is a category. The slug of the element is category2<br>This element is a category. The slug of the element is category3<br>This element is a subcategory. The slug of the element is subcategory1<br>This element is a subcategory. The slug of the element is subcategory2<br>This element is a category. The slug of the element is category555<br>This element is a subcategory. The slug of the element is subcategory666<br>This element is a category. The slug of the element is category777<br><pre>Array
(
[domain1] => Array
(
[category1] => Array
(
[0] => subcategory1
[1] => subcategory2
)
[category2] => Array
(
)
[category3] => Array
(
)
)
[domain2] => Array
(
[category555] => Array
(
[0] => subcategory666
)
)
[domain3] => Array
(
[category777] => Array
(
)
)
)
</pre>
Parse error: syntax error, unexpected '[' in /in/DKH3G on line 22
Process exited with code 255.
Output for 5.1.0 - 5.1.2
Strict Standards: var: Deprecated. Please use the public/private/protected modifiers in /in/DKH3G on line 11
Strict Standards: var: Deprecated. Please use the public/private/protected modifiers in /in/DKH3G on line 12
Strict Standards: var: Deprecated. Please use the public/private/protected modifiers in /in/DKH3G on line 13
Parse error: syntax error, unexpected '[' in /in/DKH3G on line 22
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Strict Standards: var: Deprecated. Please use the public/private/protected modifiers in /in/DKH3G on line 11
Strict Standards: var: Deprecated. Please use the public/private/protected modifiers in /in/DKH3G on line 12
Strict Standards: var: Deprecated. Please use the public/private/protected modifiers in /in/DKH3G on line 13
Parse error: parse error, unexpected '[' in /in/DKH3G on line 22
Process exited with code 255.