NYCPHP Meetup

NYPHP.org

[nycphp-talk] Getting the number of dimensions of an array

Donald Organ dorgan at donaldorgan.com
Fri May 18 17:03:33 EDT 2012


http://us.php.net/count

$size = count($array, 1);

On Fri, May 18, 2012 at 4:54 PM, Anthony Ferrara <ircmaxell at gmail.com>wrote:

> Another trick would be to not use function recursion, but a recursive
> iterator:
>
> http://codepad.viper-7.com/9PeM9c
>
> function maxDepth(array $a) {
>        $it2 = new RecursiveIteratorIterator(
>                new RecursiveArrayIterator($a)
>        );
>        $max = 0;
>        foreach ($it2 as $val) {
>                $max = max($max, $it2->getDepth());
>        }
>        return $max;
> }
>
> It will actually process less data, since the
> RecursiveIteratorIterator mode is to skip non-leaf nodes (in other
> words, elements that are arrays).  So the max() call will only ever be
> called for non-array values...  A micro-optimization for sure, but
> something worth noting...
>
> Anthony
>
>
> On Fri, May 18, 2012 at 4:48 PM, justin <justin at justinhileman.info> wrote:
> > function array_depth($el) {
> >    return is_array($el) ? max(array_map('array_depth', $el)) + 1 : 0;
> > }
> >
> > var_dump(array_depth($array));
> >
> >
> > -- justin
> >
> >
> >
> > On Fri, May 18, 2012 at 1:27 PM, Joey Derrico <joeyd473 at gmail.com>
> wrote:
> >> I am trying to count the number of dimensions in an array. I used my
> >> google-fu and came up with answers that don't actually work because I
> can
> >> have multi-dimensional array's where one int he middle is multi and the
> one
> >> after not. Below is my latest set of test code (I have been playing
> with it
> >> for hours (with varying results), none correct), It currently returns 15
> >> dimensions and should be returning 5
> >>
> >> <?php
> >> echo "\n";
> >> $test = new test();
> >>
> >> $array = array();
> >> $array[0] = '[0]';
> >> $array[1][0] = '[1][0]';
> >> $array[1][1] = '[1][1]';
> >> $array[2][0][0] = '[2][0][0]';
> >> $array[2][0][1] = '[2][0][1]';
> >> $array[2][1][0] = '[2][1][0]';
> >> $array[2][1][1] = '[2][1][1]';
> >> $array[3][0][0][0] = '[3][0][0][0]';
> >> $array[3][0][0][1] = '[3][0][0][1]';
> >> $array[3][1][0][0] = '[3][1][0][0]';
> >> $array[3][1][0][1] = '[3][1][0][1]';
> >> $array[3][1][1][0] = '[3][1][1][0]';
> >> $array[3][1][1][1] = '[3][1][1][1]';
> >> $array[4][0][0][0][0] = '[4][0][0][0][0]';
> >> $array[4][0][0][0][1] = '[4][0][0][0][1]';
> >> $array[4][1][0][0][0] = '[4][1][0][0][0]';
> >> $array[4][1][0][0][1] = '[4][1][0][0][1]';
> >> $array[4][1][1][0][0] = '[4][1][1][0][0]';
> >> $array[4][1][1][0][1] = '[4][1][1][0][1]';
> >> $array[4][1][1][1][0] = '[4][1][1][1][0]';
> >> $array[4][1][1][1][1] = '[4][0][0][0][1]';
> >> $array[5][0][0][0] = '[5][0][0][0]';
> >> $array[5][0][0][1] = '[5][0][0][1]';
> >> $array[5][1][0][0] = '[5][1][0][0]';
> >> $array[5][1][0][1] = '[5][1][0][1]';
> >> $array[5][1][1][0] = '[5][1][1][0]';
> >> $array[5][1][1][1] = '[5][1][1][1]';
> >> $array[6][0][0] = '[6][0][0]';
> >> $array[6][0][1] = '[6][0][1]';
> >> $array[6][1][0] = '[6][1][0]';
> >> $array[6][1][1] = '[6][1][1]';
> >> $array[7][0] = '[7][0]';
> >> $array[7][1] = '[7][1]';
> >> $array[8] = '[8]';
> >>
> >> echo 'The array has '.$test->countNumberOfDimensionsOfAnArray($array)."
> >> dimensions\n";
> >> //echo print_r($array,TRUE);
> >>
> >>
> >> class test
> >> {
> >>     public function isMultDimensionalArray($array)
> >>     {
> >>         $return = FALSE;
> >>
> >>         if(is_array($array)){
> >>             foreach($array as $value){
> >>                 if(is_array($value)){
> >>                     $return = TRUE;
> >>                 }
> >>             }
> >>         }
> >>         return $return;
> >>     }
> >>
> >>     public function countNumberOfDimensionsOfAnArray($array)
> >>     {
> >>         $dimensions = 0;
> >>
> >>         if(is_array($array)){
> >>             $dimensions++;
> >>             foreach($array as $key=>$value){
> >>                 if($this->isMultDimensionalArray($value)){
> >>                     $dimensions = $dimensions +
> >> $this->countNumberOfDimensionsOfAnArray($value);
> >>                 }
> >>             }
> >>         }
> >>         return $dimensions;
> >>     }
> >> }
> >> ?>
> >>
> >> Joey Derrico
> >>
> >> _______________________________________________
> >> New York PHP User Group Community Talk Mailing List
> >> http://lists.nyphp.org/mailman/listinfo/talk
> >>
> >> http://www.nyphp.org/show-participation
> >
> >
> >
> > --
> > http://justinhileman.com
> > _______________________________________________
> > New York PHP User Group Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > http://www.nyphp.org/show-participation
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show-participation
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20120518/152291e5/attachment.html>


More information about the talk mailing list