#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Convert a simple array of files to an associated array


    I'm trying to take an array of files (it's not an actual file system, just a list of files) that are in an array like:

    PHP Code:
    $assets = array(
        
    => array(
            
    'id'   => '1',
            
    'name' => 'foo/bar.php',
            
    'type' => 'php',
            
    'code' => '....'
        
    ),
        
    => array(
            
    'id'   => '3',
            
    'name' => 'lib/hello.php',
            
    'type' => 'php',
            
    'code' => '....'
        
    ),
        
    => array(
            
    'id'   => '8',
            
    'name' => 'lib/foo2/bar.php',
            
    'type' => 'php',
            
    'code' => '....'
        
    )
    ); 
    The keys are the id of that file saved in the database but can really disregard the keys, the array is the data of that file. I need to convert it to an associated array that simulates a file structure that would be like:

    PHP Code:
    $tree = array(
        array(
            
    'text'     => 'foo',
            
    'children' => array(
                array(
                    
    'id'   => '1',
                    
    'name' => 'foo/bar.php',
                    
    'type' => 'php',
                    
    'code' => '....',
                    
    'leaf' => TRUE
                
    )
            )
        ),
        array(
            
    'text'     => 'lib',
            
    'children' => array(
                array(
                    
    'text'     => 'foo',
                    
    'children' => array(
                        array(
                            
    'id' => '8',
                            
    'name' => 'lib/foo2/bar.php',
                            
    'type' => 'php',
                            
    'code' => '....',
                            
    'leaf' => TRUE
                        
    )
                    )
                ),
                array(
                    
    'id'   => '3',
                    
    'name' => 'lib/hello.php',
                    
    'type' => 'php',
                    
    'code' => '....',
                    
    'leaf' => TRUE
                
    )
            )
        )
    ); 
    I've tried a few things, the closest I've gotten is this:

    PHP Code:
    $tree = array();

    foreach (
    $assets as $asset) {
        
    $array     explode('/'$asset['name']);
        
    $leaf      array_pop($array);
        
    $assetTree = &$tree;

        foreach (
    $array as $node) {
            
    $count count($assetTree);

            if (!isset(
    $assetTree[$count])) {
                
    $assetTree[$count] = array(
                    
    'text'     => $node,
                    
    'children' => array()
                );
            }

            
    $assetTree = &$assetTree[$count]['children'];
        }

        
    $assetTree[] = array(
            
    'text' => $leaf,
            
    'leaf' => TRUE
        
    );

    This achieved me this:

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    text] => foo
                
    [children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    text] => bar.php
                                
    [leaf] => 1
                            
    )
                    )
            )
        [
    1] => Array
            (
                [
    text] => lib
                
    [children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    text] => hello.php
                                
    [leaf] => 1
                            
    )
                    )
            )
        [
    2] => Array
            (
                [
    text] => lib
                
    [children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    text] => foo2
                                
    [children] => Array
                                    (
                                        [
    0] => Array
                                            (
                                                [
    text] => bar.php
                                                
    [leaf] => 1
                                            
    )
                                    )
                            )
                    )
            )

    It's "close" but not right. The files are nested correctly but the lib "directories" are not merged together.

    Can someone help point me in a better direction? This is the first time using references also so I could be using them wrong.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    I just tried this:

    PHP Code:
    $tree = array();

    foreach (
    $assets as $asset) {
        
    $name        $asset['name'];
        
    $nameArray   explode('/'$name);
        
    $leafPart    array_pop($nameArray);
        
    $parentArray = &$tree;

        foreach (
    $nameArray as $node) {
            if (isset(
    $parentArray['children'])) {
                
    $parentArray['children'][$node] = array(
                    
    'text'     => $node,
                    
    'children' => array()
                );

                
    $parentArray = &$parentArray['children'][$node];
            } else {
                
    $parentArray[$node] = array(
                    
    'text'     => $node,
                    
    'children' => array()
                );

                
    $parentArray = &$parentArray[$node];
            }
        }

        
    $parentArray['children'][] = array(
            
    'text' => $leafPart,
            
    'leaf' => TRUE
        
    );

    But this is setting keys to nest things and it's also overwriting the lib directory. I get this:

    PHP Code:
    Array
    (
        [
    foo] => Array
            (
                [
    text] => foo
                
    [children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    text] => bar.php
                                
    [leaf] => 1
                            
    )
                    )
            )
        [
    lib] => Array
            (
                [
    text] => lib
                
    [children] => Array
                    (
                        [
    foo2] => Array
                            (
                                [
    text] => foo2
                                
    [children] => Array
                                    (
                                        [
    0] => Array
                                            (
                                                [
    text] => bar.php
                                                
    [leaf] => 1
                                            
    )
                                    )
                            )
                    )
            )

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Are u sure about doing this? Not a very normal way of managing data at all. What will u do as your data grows? You mention a db. Why arent u using that to put this stuff into a simple table? So Much More Simple than 3 level nssted array!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    Unfortunately I have to deal with this database and it's data, it's been deemed too costly and risky (not my words) to convert to anything and must stay as-is.

    The array is a representation of exactly I get back from MySQL.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    What is the problem you're trying to solve? Why do you believe re-organizing this into a hierarchical structure is going to help you?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    The structure that I'm trying to get to is to deliver it to the client that wants it in this structure and not knowing the client computer and how old it is, I'm trying to do the parsing server side.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    So nobody has told YOU the problem either? You just have a homework assignment to do?

    This structure you're going for is inefficient and not really all that usable. If you knew what they needed it for and why they've decided on this, maybe you could solve it properly instead of taking one bad system and turning it into another bad system.

    If you honestly have no insight into this task, I'll try to work on your array this afternoon.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    That structure is wanted to feed to a Tree component in Ext JS. I understand that the data that I have to work with is not efficient but I can only work with what I have.
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Ah, ExtJS

    I have a love-hate relationship with this library; and the hate part is due to things like this.

    I can't say that I have solved it, but I have seen one solution, which to serve 1 level at a time, with an iterator in JS to request children wherever there is a leaf=false node.

    If you don't have control of the JS, then one monolithic JSON/XML file containing the hierarchy will be necessary
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Does this thread help? I know it's not a direct answer, but I think Dan's solution with the class method and by reference "&" is relevant to your solution

    http://forums.devshed.com/php-develo...al-654144.html
    Last edited by Northie; August 1st, 2013 at 11:03 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Try this:
    PHP Code:
    <?php
     $assets 
    = array( 
        
    => array( 
            
    'id'   => '1'
            
    'name' => 'foo/bar.php'
            
    'type' => 'php'
            
    'code' => '....' 
        
    ), 
        
    => array( 
            
    'id'   => '3'
            
    'name' => 'lib/hello.php'
            
    'type' => 'php'
            
    'code' => '....' 
        
    ), 
        
    => array( 
            
    'id'   => '8'
            
    'name' => 'lib/foo2/bar.php'
            
    'type' => 'php'
            
    'code' => '....' 
        

    );  


    function 
    addNode$path, &$tree$asset ) {
      
    $current array_shift($path);
      if ( !isset(
    $tree[$current]) ) {
        
    $tree[$current] = array( 'text' => $current'children' => array() );
        
    $tree[$current]['children'][] = $asset;
        return;
      }
      if ( 
    count($path) == ) {
        
    $tree[$current]['children'][] = $asset;
        return;
      }
      
    addNode$path$tree[$current]['children'], $asset );  
    }

    $tree = array();

    foreach ( 
    $assets as $asset ) { 
      
    $path explode('/'$asset['name']);
      
    array_pop($path);
      
    $asset['leaf'] = true;
      
    addNode$path$tree$asset );
    }

    //optional
    $tree array_values($tree);

    //debug
    print_r($tree);
    Outputs:
    [CODE]Array
    (
    [0] => Array
    (
    [text] => foo
    [children] => Array
    (
    [0] => Array
    (
    [id] => 1
    [name] => foo/bar.php
    [type] => php
    [code] => ....
    [leaf] => 1
    )

    )

    )

    [1] => Array
    (
    [text] => lib
    [children] => Array
    (
    [0] => Array
    (
    [id] => 3
    [name] => lib/hello.php
    [type] => php
    [code] => ....
    [leaf] => 1
    )

    [foo2] => Array
    (
    [text] => foo2
    [children] => Array
    (
    [0] => Array
    (
    [id] => 8
    [name] => lib/foo2/bar.php
    [type] => php
    Code:
     => ....
                                                [leaf] => 1
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )

    Comments on this post

    • mitchellsimoens agrees : This is exactly what I needed. Thank you very much!
    Last edited by ManiacDan; August 1st, 2013 at 11:15 AM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Originally Posted by Northie
    Does this thread help? I know it's not a direct answer, but I think Dan's solution with the class method and by reference "&" is relevant to your solution

    http://forums.devshed.com/php-develo...al-654144.html
    Kind of like what I did for you way back when, but this one uses recursion AND references, rather than just tricky references. Slightly cleaner.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo