SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [PHP] Nested array, multidimensional

    Hello guys.

    Got an array

    PHP Code:
    Array
    (
        [
    cats] => Array
            (
                [
    1] => Array
                    (
                        [
    id] => 1
                        
    [name] => women
                        
    [pID] => 0
                    
    )

                [
    2] => Array
                    (
                        [
    id] => 2
                        
    [name] => fashion
                        
    [pID] => 1
                    
    )

                [
    3] => Array
                    (
                        [
    id] => 3
                        
    [name] => sport
                        
    [pID] => 2
                    
    )

                [
    4] => Array
                    (
                        [
    id] => 4
                        
    [name] => home
                        
    [pID] => 1
                    
    )

                [
    5] => Array
                    (
                        [
    id] => 5
                        
    [name] => men
                        
    [pID] => 4
                    
    )

            )



    using

    PHP Code:
    $arr = array();
       foreach(
    $catsL['cats'] as $k => $c) {
         if((
    $c['pID']) > 0) {
             
    $array[$c['pID']]['children'][] = $c;
         } else {
             
    $array[$c['id']] = $c;
         }

    Want to get array like:

    item/id/parent

    item-1 / 2 / 0
    item-1 / 3 / 2 - child of element with id 2
    item-1 / 4 / 3 - child of element with id 3


    Got:

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    id] => 1
                
    [name] => women
                
    [pID] => 0
                
    [children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    id] => 2
                                
    [name] => fashion
                                
    [pID] => 1
                            
    )

                        [
    1] => Array
                            (
                                [
    id] => 4
                                
    [name] => home
                                
    [pID] => 1
                            
    )

                    )

            )

        [
    1] => Array
            (
                [
    children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    id] => 3
                                
    [name] => sport
                                
    [pID] => 2
                            
    )

                    )

            )

        [
    2] => Array
            (
                [
    children] => Array
                    (
                        [
    0] => Array
                            (
                                [
    id] => 5
                                
    [name] => men
                                
    [pID] => 4
                            
    )

                    )

            )


    Childs getting inserted as new elements of 1st level array not as child.

    Any clues?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are making the solving of this issue a bit harder for us than is necessary.

    Instead of showing the result of (I think)
    PHP Code:
    var_dump($catsL); 
    Show the result of:
    PHP Code:
    echo var_export($catsL1); 
    Then instead of showing the expected result using a completely different nomenclature ("item1")
    Code:
    item-1 / 2 / 0
    item-1 / 3 / 2 - child of element with id 2
    item-1 / 4 / 3 - child of element with id 3
    Show us EXACTLY what you want - in the manner of the var_export() result, ie the exact makeup of the expected computed array.

    There are many who can do this kind of modelling in their heads, but I am not one of them, I need a test case I can copy/paste and concrete result to which I can aim -- then I will have a go.

    I don't think I am alone though, as your Q has gone unanswered in ~16hrs

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code, the array you begin with, and the array you end up with just plain doesnt make sense.

    Code:
    [1] => Array 
                    ( 
                        [id] => 1 
                        [name] => women 
                        [pID] => 0 
                    )
    Here, pID is 0.

    Which means your else-block should run...
    Code:
      $array[$c['id']] = $c;
    Yet, your resulting array is not using
    Code:
    $c['id']
    as its key here:

    Code:
    [0] => Array 
            ( 
                [id] => 1 
                [name] => women 
                [pID] => 0 
                [children] => Array 
                    (
    It's using zero?
    What truly baffles me... The two arrays in children manages to plop into the "right" place, while the key is wrong!
    Bafflement, somethings wrong, and i cant see what.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •