Array woes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Array woes

Post by infolock »

So, here is my issue.

Say I have the following array:

Code: Select all

array('home' => array(0 => 'jon.html',
                                   'site' => array(0 => 'jon.html'),
                                   ),
          'next'  => array('site' => array(0 => 'jon.html',
                                                          'site' => array(0 => 'jon.html'),
                                                         ),
                                   ),
        );
What I want to do, is track where I am in the array at all times. The issue is that I'm using a recursive function to loop through it, so everytime I find an array within my array, it calls itself back to loop through the array.

Code: Select all

function show_file_structure($array, $key_name = '', $is_parent = 1) {
  global $array_map, $global_key;
  if(!empty($key_name)) {
    show_dir_tree_display($key_name);
  }
  if(is_array($array)) {
    sort_array($array);
    foreach($array as $key => $value) {
      if(is_array($value)) {
        sort_array($value);
        show_dir_tree_display($key);
        foreach($value as $sub_key => $sub_value) {
          if(!is_array($sub_value) && !empty($sub_value)) {
            echo "\n\n<input style=\"margin-right: 10px;\" type=\"checkbox\" name=\"$sub_value\" /> $sub_value<br>\n\n";
          } elseif(is_array($sub_value)) {
            show_file_structure($sub_value, $sub_key, 0);
          }
        }
        echo "</div>";
      } else {
        echo "\n\n<input type=\"checkbox\" name=\"$value\" /> $value<br>\n\n";
      }
    }
  }
  echo "</div>";
}

It loops fine and all that. that's not the problem. the problem is i want to keep track where in the filesystem array I am, so that I can map my input checkboxes to have the path as the name and the filename as the value.

ie, i'd like to have my checkboxes look like:

Code: Select all

<input type="checkbox" name="/htdocs/next/site/" value="jon.html" />
<input type="checkbox" name="/htdocs/" value="jon.html" />
<input type="checkbox" name="/htdocs/site/" value="jon.html" />
and so on. i'm trying to do this within the above function, but i can't seem to keep track where I am in the array! any help would be much appreciated...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Non-recursive array code usually saves references (often in a stack) to save the point in the tree that is your current position.
(#10850)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

yep... i know.

but unfortunately i'm passing the sub_key of the array to the function. so the next time I loop through the array passed, I don't have all the parent information available.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

n/m, you can close this. i just figured it out.


ya just gotta keep an array map going..

like, add a new key to the array, and when you are finished using it, pop it off.
Post Reply