3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ 0 => [ "category_id" => 20, "category_name" => "Category 1" , "sub_categories" => [ 0 => [ "category_id" => 25, "category_name" => "Category 1.1", "sub_categories" => '' ], 1=> [ "category_id" => 26, "category_name" => "Category 1.2", "sub_categories" => '' ], 2 => [ "category_id" => 27, "category_name" => "Category 1.3", "sub_categories" => [ 0 => [ "category_id" => 31, "category_name" => "Category 2.1", "sub_categories" => '' ], 1=> [ "category_id" => 32, "category_name" => "Category 2.2", "sub_categories" => '' ], ] ], ] ], 1=> [ "category_id" => 21, "category_name" => "Category 2", "sub_categories" => '' ], ]; function getCats($data, array &$ar, $res = ''){ foreach($data as $arr){ $res_new = ''; $res_new .= $arr['category_name'] && $res ? $res.' -> '.$arr['category_name'] : $arr['category_name']; $ar[] = ["category_id"=>$arr['category_id'], "category_name"=>$res_new, ]; // echo $res_new.PHP_EOL; // echo .PHP_EOL; if(is_array($arr['sub_categories'])){ if (count($arr['sub_categories'])>0){ getCats($arr['sub_categories'], $ar, $res_new); } } } } $ares = []; getCats($data,$ares); print_r($ares);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [category_id] => 20 [category_name] => Category 1 ) [1] => Array ( [category_id] => 25 [category_name] => Category 1 -> Category 1.1 ) [2] => Array ( [category_id] => 26 [category_name] => Category 1 -> Category 1.2 ) [3] => Array ( [category_id] => 27 [category_name] => Category 1 -> Category 1.3 ) [4] => Array ( [category_id] => 31 [category_name] => Category 1 -> Category 1.3 -> Category 2.1 ) [5] => Array ( [category_id] => 32 [category_name] => Category 1 -> Category 1.3 -> Category 2.2 ) [6] => Array ( [category_id] => 21 [category_name] => Category 2 ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
140.25 ms | 408 KiB | 5 Q