T-Bird Posted January 3, 2009 Share Posted January 3, 2009 I am building a directory tree via array. I need to find a way to use a GET variable to navigate the tree. Here's an example: My array would be laid out as such $list = array(2) { [0] => array(2) { [‘name’] => Directory 1 [‘info’] => array(3) { [0] => array(2) { [‘name’] => File 1.a [‘info’] => foo } [1] => array(2) { [‘name’] => File 1.b [‘info’] => foo } [2] => array(2) { [‘name’] => File 1.c [‘info’] => foo } } } [1] => array(2) { [‘name’] => File 2 [‘info’] => foo } } Each directory holds one array per file. Should that file be a directory itself it holds another array for those sub directories. This can be an arbitrary depth. Now, I'm trying to take a GET variable (probably a string layed out as "3:0:1:...", but I'm up for alternative methods) that will tell the code how to navigate into the array and find the desired file. My thinking was that I would list which array index to go into so in the above string it would go to index 3, then in the array stored in that info index go to index 0 and then index 1, so-on-and-so-forth. My question: Short of an eval() function - which I don't want to use until it's my last choice since the data is coming off a user input - is there a way to tell PHP how far to navigate into an array that may be an arbitrary size in any given dimension? One last wrench in the works, I need the list to maintain state; it's in a session variable, so the process can't be destructive. Thanks in advance for the help Quote Link to comment https://forums.phpfreaks.com/topic/139275-solved-navigate-array-via-string/ Share on other sites More sharing options...
T-Bird Posted January 3, 2009 Author Share Posted January 3, 2009 I'm also open to restructuring my directory tree if that would make it easier. Regardless, I need to navigate an N-dimensional array using a string or other input. If it helps this is the last thing I tried: if(isset($_GET['dir'])) { //open/close directories as necessary $toOpen = explode(":",$_GET['dir']); $depth = count($toOpen)-1; $arr = &$_SESSION['list']; foreach($toOpen as $key => $v) { if(array_key_exists($v,$_SESSION['list'])) { if($key < $depth) { $arr = $arr[$v]['size']; } else { $arr[$v]['open'] = (int) !$arr[$v]['open']; } } } } The problem with this being that I've lost all directory data above the level of the directory I just opened since it overwrites my SESSION variable. Quote Link to comment https://forums.phpfreaks.com/topic/139275-solved-navigate-array-via-string/#findComment-728884 Share on other sites More sharing options...
sasa Posted January 3, 2009 Share Posted January 3, 2009 try <?php function navigate($path, $array){ $path = explode(':',$path); $key = array_shift($path); if (!is_array($array) or !array_key_exists($key, $array)) return 'error'; $path = implode(':',$path); if (!strlen($path)) return $array[$key]['name']; else return navigate($path, $array[$key]['info']); } $test = array( array( 'name' => 'Directory 1', 'info' => array( array( 'name' => 'File 1.a', 'info' => 'foo'), array( 'name' => 'File 1.b', 'info' => 'foo'), array( 'name' => 'File 1.c', 'info' => 'foo') ) ), array( 'name' => 'File 2', 'info' => 'foo' ) ); $path = "0:1"; echo navigate($path, $test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/139275-solved-navigate-array-via-string/#findComment-728926 Share on other sites More sharing options...
T-Bird Posted January 4, 2009 Author Share Posted January 4, 2009 Well, that works good - except - I left out one key piece of information :-X. I'm not having trouble reading the array, I'm having trouble writing too it. The only way I can yet discern to write to an arbitrary point within an array involves chopping the array into smaller pieces until I get to the deepest sub directory. However, I need the array to maintain state and am storing it in a SESSION variable so once the array is chopped up it serves me no purpose. I'll keep thinking on it, and hoping someone here can figure something out. Thanks for the help and thanks in advance again for further support. Quote Link to comment https://forums.phpfreaks.com/topic/139275-solved-navigate-array-via-string/#findComment-729211 Share on other sites More sharing options...
T-Bird Posted January 4, 2009 Author Share Posted January 4, 2009 Sorry to be a bother, I can't find the edit button ><. Anyways, the answer was right in front of me the whole time. By passing the sub directory arrays by reference I can make changes to the primary array without overwriting it. In the code in my second post I simply changed the line that read: $arr = $arr[$v]['size']; With: $arr = &$arr[$v]['size']; Now it works like a charm. Thanks to all who helped, by post or by thought. Quote Link to comment https://forums.phpfreaks.com/topic/139275-solved-navigate-array-via-string/#findComment-729251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.