/** */ function arrayToTree($array, $checked_list = false, $tree_id = false) { if ($tree_id) { $result = "<ul id=\"{$tree_id}\">"; } else { $result = "<ul>"; } foreach ($array as $level_one => $next_level) { if (!preg_match('/^\\./', $level_one)) { if ($checked_list != false) { if ($checked_list[0] == $level_one) { $checked = " checked='checked' "; } else { $checked = "{$checked_list['0']}=\"{$level_one}\""; } } $result = $result . " <li><input type=\"checkbox\" name=\"folders[]\" \n\t\t\t\tvalue=\"{$level_one}\" {$checked} >" . $level_one; if (sizeof($next_level) != 0) { if (isset($checked_list)) { $new_checked_list = array_slice($checked_list, 1); } $result = $result . arrayToTree($next_level, $new_checked_list); } } } $result = $result . "</ul>"; return $result; }
/** * Change an array with 'key.sub_key.final_key' keys into a tree with[key][sub_key][final_key] * * @param $array mixed[] * @param $recurse boolean * @return mixed[] */ function arrayToTree($array, $recurse = true) { $result = []; $sub_arrays = []; foreach ($array as $key => $value) { if (strpos($key, DOT) !== false) { list($super_key, $sub_key) = explode(DOT, $key, 2); $sub_arrays[$super_key][$sub_key] = $value; } else { $result[$key] = $value; } } foreach ($sub_arrays as $key => $sub_array) { $result[$key] = $recurse ? arrayToTree($sub_array) : $sub_array; } return $result; }
<?php // echo($mainFoldersArrayScript); // echo($subFoldersArrayScript); ?> <form name="gitFilesForm" action="<?php $PHP_SELF; ?> " method="POST"> <div class="file_status" > <?php echo $status_message; ?> </div> <div class="folder_tree"> <?php echo arrayToTree($directories, $folders, "folder_tree"); ?> </div> <div class="group_settings"> <label for="main_cat">Main Folder:</label> <select name="main_cat" onchange="updateSubFolders(this.form)"> </select><br/> <label for="sub_cat">Sub Folder:</label> <select name="sub_cat"> </select></br> <label for="file_folder">Folder Name:</label> <input type="text" id="file_folder" name="file_folder" size="70" class="file_folder" value="<?php echo $_POST['file_folder']; ?> " /><br/>