3v4l.org

run code in 300+ PHP versions simultaneously
<?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>";
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
<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>
Output for 4.4.2 - 4.4.9, 5.1.3 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
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.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected '[' in /in/DKH3G on line 22
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/DKH3G on line 22
Process exited with code 255.

preferences:
285.93 ms | 401 KiB | 459 Q