From: "Daevid Vincent" on 15 Mar 2010 19:45 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?
From: Ashley Sheridan on 15 Mar 2010 19:43 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
From: "Daevid Vincent" on 15 Mar 2010 20:23 Oh. I know it's not a simple solution to do right Ashley. And exacerbated by the fact that each array dimension can have different dimensions as well. This is why I wanted someone else's solution first before I spend hours or days on one that works reliably. :) _____ From: Ashley Sheridan [mailto:ash(a)ashleysheridan.co.uk] Sent: Monday, March 15, 2010 4:44 PM Subject: Re: [PHP] Need routine to tell me number of dimensions in array 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.
From: Ashley Sheridan on 15 Mar 2010 20:20 On Mon, 2010-03-15 at 17:23 -0700, Daevid Vincent wrote: > Oh. I know it's not a simple solution to do right Ashley. And exacerbated > by the fact that each array dimension can have different dimensions as > well. This is why I wanted someone else's solution first before I spend > hours or days on one that works reliably. :) > > > _____ > > From: Ashley Sheridan [mailto:ash(a)ashleysheridan.co.uk] > Sent: Monday, March 15, 2010 4:44 PM > Subject: Re: [PHP] Need routine to tell me number of dimensions in array > > 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. > > Best way I can think of is to iterate the entire thing and keep a count as you do. I'm not aware of any functions that can do what you need there. Thanks, Ash http://www.ashleysheridan.co.uk
From: Jim Lucas on 15 Mar 2010 20:33
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"... > How about this... Using a slightly modified array that you posted, I came up with this in about 10 minutes <pre>I am working with the following data structure <?php $in = array( 0 => array( 0 => array('Flight Number', 'flight_number'), 1 => array( 0 => array('Timestamp Departure', 'timestamp_departure'), 1 => array('Timestamp Arrival', 'timestamp_arrival'), ) ), 1 => array('Departure City', 'departure_city'), 2 => array('Arrival City', 'arrival_city'), ); print_r($in); echo "\n\n"; $max_depth = 0; $cur_depth = 0; function max_array_depth($ar) { global $cur_depth, $max_depth; if ( is_array($ar) ) { $cur_depth++; if ( $cur_depth > $max_depth ) { $max_depth = $cur_depth; } foreach ( $ar AS $row ) { max_array_depth($row); } $cur_depth--; } } max_array_depth($in); echo "Max depth of array is: {$max_depth}"; ?></pre> http://www.cmsws.com/examples/php/testscripts/daevid(a)daevid.com/0002.php -- Jim Lucas NOC Manager 541-323-9113 BendTel, Inc. http://www.bendtel.com |