index an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    index an array

    Hi,

    Code:
    var_dump($files)
    returns

    array(4) { [0]=> array(4) { [0]=> array(4) { [0]=> array(0) { } ["file_name"]=> string(51) "01_04_2016.doc " ["file_size"]=> float(138) ["file_type"]=> string(18) "applicatio n/msword" } ["file_name"]=> string(63) "09_04_2016.doc " ["file_size"]=> float(143) ["file_type"]=> string(18) "applicatio n/msword" } ["file_name"]=> string(51) "07_04_2016.doc " ["file_size"]=> float(143.5) ["file_type"]=> string(18) "applicatio n/msword" }

    How do I index such array in a loop?

    Thank You
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that array is already indexed.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      i meant how do i loop through such array

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        usually you would loop through an array using foreach, but your array is created in an odd way that makes it hard to use.

        your array with indentation
        Code:
        array(4) { 
            [0]=> array(4) { 
                [0]=> array(4) { 
                    [0]=> array(0) { 
                    } 
                    ["file_name"]=> string(51) "01_04_2016.doc" 
                    ["file_size"]=> float(138) 
                    ["file_type"]=> string(18) "application/msword" 
                } 
                ["file_name"]=> string(63) "09_04_2016.doc" 
                ["file_size"]=> float(143) 
                ["file_type"]=> string(18) "application/msword" 
            } 
            ["file_name"]=> string(51) "07_04_2016.doc" 
            ["file_size"]=> float(143.5) 
            ["file_type"]=> string(18) "application/msword" 
        }
        as you can see, the data are put into different levels, which make it hard to work with. it would be easier to have the data in a more sensibly organised way, e.g.
        Code:
        array(3) { 
            [0]=> array(3) { 
                ["file_name"]=> string(51) "01_04_2016.doc" 
                ["file_size"]=> float(138) 
                ["file_type"]=> string(18) "application/msword" 
            } 
            [1]=> array(3) { 
                ["file_name"]=> string(63) "09_04_2016.doc" 
                ["file_size"]=> float(143) 
                ["file_type"]=> string(18) "application/msword" 
            } 
            [2]=> array(3) { 
                ["file_name"]=> string(51) "07_04_2016.doc" 
                ["file_size"]=> float(143.5) 
                ["file_type"]=> string(18) "application/msword" 
            }
        }
        eventually, I recommend to fixd this' array creation before you proceed with the array data.

        Comment

        Working...