Re: Need routine to tell me number of dimensions in array.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2010-03-15 at 16:45 -0700, Daevid Vincent wrote:

> Anyone have a function that will return an integer of the number of
> dimensions an array has?
> 
> I did some quick searches and came up with nothing. 
> The closest was here of someone asking the same thing, but his solution
> isn't right:
> http://www.bigresource.com/PHP-count-array-dimensions-VrIahx1b.html
> http://php.net/manual/en/function.count.php
> 
> From a human standpoint, it's easy to see, "oh, this is a TWO
> dimensional"...
> 
> Array
> (
>     [0] => Array
>         (
>             [0] => Data marked as faulty or timestamps before 2005 or in
> the future (2035)
>             [1] => bad_data
>         )
> 
>     [1] => Array
>         (
>             [0] => Hardware Part Numbers
>             [1] => hardware_part_numbers
>         )
> 
>     [2] => Array
>         (
>             [0] => Software Part Numbers
>             [1] => software_part_numbers
>         )
> 
>     [3] => Array
>         (
>             [0] => Software Version Number
>             [1] => software_version_numbers
>         )
> 
>     [4] => Array
>         (
>             [0] => Configuration Part Numbers
>             [1] => configuration_part_numbers
>         )
> 
> )
> 
> From a programatic POV, it's not quite that easy to see "this is a THREE
> dimensional", since element [0][0] is missing and it actually starts at
> [0][1], so you can't do an is_array($foo[0][0]) on it (but if you did on
> [0][1] it would pass) so you have to itterate over ALL elements of the
> array until you find one that hits or you exhaust that dimension. But then
> you have to traverse any subdimensions too, most likely recursively.
> 
> Array
> (
>     [0] => Array
>         (
>             [1] => Array
>                 (
>                     [0] => Aircraft Registration
>                     [1] => aircraft_registration
>                 )
> 
>             [2] => Array
>                 (
>                     [0] => Aircraft Type-Subtype
>                     [1] => aircraft_type_subtype
>                 )
> 
>             [3] => Array
>                 (
>                     [0] => System
>                     [1] => system_type_name
>                 )
> 
>             [4] => Array
>                 (
>                     [0] => Flight Count
>                     [1] => flight_count
>                 )
> 
> ...
> 
> 
> Then it gets even more complex as this has all sorts of holes in it...
> 
> Array
> (
>     [0] => Array
>         (
>             [0] => Array
>                 (
>                     [0] => Flight Number
>                     [1] => flight_number
>                 )
> 
>             [4] => Array
>                 (
>                     [0] => Timestamp Departure
>                     [1] => timestamp_departure
>                 )
> 
>             [6] => Array
>                 (
>                     [0] => Timestamp Arrival
>                     [1] => timestamp_arrival
>                 )
> 
>             [8] => Array
>                 (
>                     [0] => Departure City
>                     [1] => departure_city
>                 )
> 
>             [9] => Array
>                 (
>                     [0] => Arrival City
>                     [1] => arrival_city
>                 )
> 
> Now I could take the time to dig in and figure this all out, but I thought
> maybe someone already had a solution they could share?
> 
> 


The only way to do it reliably would be to iterate the entire array,
element by element, as all the elements of an array might not
necessarily be all of the array type or int's.

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux