$struc = ''; foreach ($parent as $parent_l) { $struc .= "[$parent_l]['child']"; } $xarray[$struc] = array("catid"=>$xr['id'],"category"=>$xr['category'],"child"=>'');