$item = explode("_", stripslashes($_REQUEST['item'])); $x = 0; $_SESSION['level'] = 4; if($x <= count($_SESSION['level'])) { $i .= $_REQUEST['item'].'_'; } -------------- //$_REQUEST['item'] = '1_1_1_1_1_1_1_1_1'; $item = explode("_", $_REQUEST['item']); $_SESSION['level'] = 4; for( $i=0 $i < $_SESSION['level']; $i++ ) { $i .= $item[$i]."_"; } //this will leave you with 1_1_1_1_ //this removes the trailing _ $i = substr($i, 0, -1);