423,319 Members | 2,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,319 IT Pros & Developers. It's quick & easy.

index an array

oll3i
100+
P: 679
Hi,

Expand|Select|Wrap|Line Numbers
  1. var_dump($files)
  2.  
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) "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" }

How do I index such array in a loop?

Thank You
Jun 5 '16 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
that array is already indexed.
Jun 6 '16 #2

oll3i
100+
P: 679
i meant how do i loop through such array
Jun 6 '16 #3

Dormilich
Expert Mod 5K+
P: 8,639
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
Expand|Select|Wrap|Line Numbers
  1. array(4) { 
  2.     [0]=> array(4) { 
  3.         [0]=> array(4) { 
  4.             [0]=> array(0) { 
  5.             } 
  6.             ["file_name"]=> string(51) "01_04_2016.doc" 
  7.             ["file_size"]=> float(138) 
  8.             ["file_type"]=> string(18) "application/msword" 
  9.         } 
  10.         ["file_name"]=> string(63) "09_04_2016.doc" 
  11.         ["file_size"]=> float(143) 
  12.         ["file_type"]=> string(18) "application/msword" 
  13.     } 
  14.     ["file_name"]=> string(51) "07_04_2016.doc" 
  15.     ["file_size"]=> float(143.5) 
  16.     ["file_type"]=> string(18) "application/msword" 
  17. }
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.
Expand|Select|Wrap|Line Numbers
  1. array(3) { 
  2.     [0]=> array(3) { 
  3.         ["file_name"]=> string(51) "01_04_2016.doc" 
  4.         ["file_size"]=> float(138) 
  5.         ["file_type"]=> string(18) "application/msword" 
  6.     } 
  7.     [1]=> array(3) { 
  8.         ["file_name"]=> string(63) "09_04_2016.doc" 
  9.         ["file_size"]=> float(143) 
  10.         ["file_type"]=> string(18) "application/msword" 
  11.     } 
  12.     [2]=> array(3) { 
  13.         ["file_name"]=> string(51) "07_04_2016.doc" 
  14.         ["file_size"]=> float(143.5) 
  15.         ["file_type"]=> string(18) "application/msword" 
  16.     }
  17. }
eventually, I recommend to fixd this' array creation before you proceed with the array data.
Jun 6 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.