Page 1 of 1
How to iterate through associative arrays
#1
How to iterate through associative arrays
Posted 28 June 2007 - 08:31 PM
array ( 'directory_structure.php' => 831, 'excursion_doc' => array ( 'excursion_doc_1_Winter.jpg' => 105542, 'excursion_doc_2_test.doc' => 24064, 'excursion_doc_7_test.doc' => 24064, ), 'family_agreement' => array ( 'family_agreement_1_Winter.jpg' => 105542, 'family_agreement_2_test.doc' => 24064, 'family_agreement_7_test.doc' => 24064, ), 'filming_doc' => array ( 'filming_doc_1_Winter.jpg' => 105542, 'filming_doc_2_test.doc' => 24064, 'filming_doc_7_test.doc' => 24064, ), 'hydrotherapy_swimming_doc' => array ( 'hydro_swim_doc_1_Winter.jpg' => 105542, 'hydro_swim_doc_2_test.doc' => 24064, 'hydro_swim_doc_7_test.doc' => 24064, ), 'immunization_card' => array ( 'immunization_card_1_Winter.jpg' => 105542, 'immunization_card_2_Sample2.PDF' => 50217, 'immunization_card_7_test.doc' => 24064, ), 'immunization_consent_doc' => array ( 'immunizaion_doc_1_Winter.jpg' => 105542, 'immunizaion_doc_2_Sample2.PDF' => 50217, 'immunizaion_doc_7_test.doc' => 24064, ), 'iterating_associative_arrays.php' => 2759, 'medical_history_and_treatment_approval_form' => array ( 'med_history_doc_1_Winter.jpg' => 105542, 'med_history_doc_2_Sample2.PDF' => 50217, 'med_history_doc_7_test.doc' => 24064, ), 'medical_reports' => array ( 'medical_reports_1_Winter.jpg' => 105542, 'medical_reports_2_test.doc' => 24064, 'medical_reports_7_test.doc' => 24064, ), 'previous_school_documents' => array ( 'prev_school_document_1_Winter.jpg' => 105542, 'Winter.jpg' => 105542, ), 'test_tree_structure.php' => 1082, 'test_tree_structure_2.php' => 930, )
The above is the result as an associative array. I would want to know how to iterate through the above array(nested arrays) in order to display a tree structure.
Can anyone help me on this with a basic demo code.
I am stuck.. Urgently help needed!
Regards,
Dream2rule
The above is the result as an associative array. I would want to know how to iterate through the above array(nested arrays) in order to display a tree structure.
Can anyone help me on this with a basic demo code.
I am stuck.. Urgently help needed!
Regards,
Dream2rule
#3
Re: How to iterate through associative arrays
Posted 28 June 2007 - 10:03 PM
Styx, on 28 Jun, 2007 - 10:02 PM, said:
What do you mean display a tree structure? print_r prints a tree structure...
Beyond that, you need to provide some code to show that you've attempted this first before anyone can help you with code.
Beyond that, you need to provide some code to show that you've attempted this first before anyone can help you with code.
I have worked on the code to generate an associative array as below
<?php
function DirTree4($dir)
{
$tree = array();
$dirs = array(array($dir, &$tree));
for($i = 0; $i < count($dirs); ++$i)
{
$d = opendir($dirs[$i][0]);
$tier =& $dirs[$i][1];
while($file = readdir($d))
{
if ($file != '.' and $file != '..')
{
$path = $dirs[$i][0] . DIRECTORY_SEPARATOR . $file;
if (is_dir($path))
{
$tier[$file] = array();
$dirs[] = array($path, &$tier[$file]);
}
else
{
$tier[$file] = filesize($path);
}
}
}
}
return $tree;
}
$d = DirTree4(getcwd());
var_export($d);
?>
Using the above code i am able to generate associative array which contains the information about various folders, sub-folders and files.
Now i would like to display a tree format in order to display the folders and the contained files. Easy readable format and once i click on any folder it should display the files contained in that folder.
Regards,
Dream2rule
This post has been edited by dream2rule: 28 June 2007 - 10:04 PM
#5
Re: How to iterate through associative arrays
Posted 03 July 2007 - 07:15 AM
I think it might help you to understand recursive functions - in other words, functions which calls themselves. Your DirTree4 function could be simplified a little AND given the ability to read any level of nested directories.
I realise this doesn't help with your actual problem; however a little clue is that it will also require a recursive function.
Now I could be wrong but I think the above function is doing exactly the same thing as your original one (with the added bonus that it will process as many levels of nested directories as you like), but without all that confusing business of $tier and array(array($dir,&$tree)).
--serializer
I realise this doesn't help with your actual problem; however a little clue is that it will also require a recursive function.
<?php
function DirTree4($dir)
{
$tree = array();
$d = opendir($dir);
while($file = readdir($d))
{
if ($file != '.' and $file != '..')
{
$path = $dir . DIRECTORY_SEPARATOR . $file;
/*
Here is where the magic of recursion takes place:
If $path is a directory, then we feed it back into DirTree4 to get it's contents processed exactly the same as the parent dir. Then we add that array to our array as normal...
*/
if (is_dir($path))
{
$tree[$file] = DirTree4($file);
}
else
{
$tree[$file] = filesize($path);
}
}
}
return $tree;
}
$d = DirTree4(getcwd());
var_export($d);
?>
Now I could be wrong but I think the above function is doing exactly the same thing as your original one (with the added bonus that it will process as many levels of nested directories as you like), but without all that confusing business of $tier and array(array($dir,&$tree)).
--serializer
Page 1 of 1

Ask A New Question
Reply





MultiQuote




|