Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,670 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,171 people online right now. Registration is fast and FREE... Join Now!




How to iterate through associative arrays

 
Reply to this topicStart new topic

How to iterate through associative arrays

dream2rule
post 28 Jun, 2007 - 08:31 PM
Post #1


New D.I.C Head

*
Joined: 26 Jun, 2007
Posts: 5


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Styx
post 28 Jun, 2007 - 09:02 PM
Post #2


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

dream2rule
post 28 Jun, 2007 - 10:03 PM
Post #3


New D.I.C Head

*
Joined: 26 Jun, 2007
Posts: 5


My Contributions


QUOTE(Styx @ 28 Jun, 2007 - 10:02 PM) *

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.


I have worked on the code to generate an associative array as below

CODE


<?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 Jun, 2007 - 10:04 PM
User is offlineProfile CardPM

Go to the top of the page

Styx
post 28 Jun, 2007 - 10:50 PM
Post #4


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


Now that you have an associative array and its result, have you tried to make a tree like you've mentioned? Post what you come up with.
User is offlineProfile CardPM

Go to the top of the page

serializer
post 3 Jul, 2007 - 07:15 AM
Post #5


D.I.C Head

**
Joined: 25 Jun, 2007
Posts: 108


My Contributions


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.

CODE


<?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
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:58AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month