0

Hi,

I have a resultset which is an array with either 1 or 2 or 3 or 4 ... dimentions. How can I find out how many dimentions are returned?

Thanks

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by diafol
0
function adc($arrayVariable, $counter = 0) {
	if(is_array($arrayVariable)) {
		return adc(current($arrayVariable), $counter + 1);
	} else {
		return $counter;
	}
}

This isn't my code - can't remember where I got it - from some forum last summer - think it was Big Resource.

So:

$no_dimensions = adc($mybigarray);
0

ok i understand your problem.

Here is solution

function getArrCount ($arr, $depth=1) {
      if (!is_array($arr) || !$depth) return 0;
        
     $res=count($arr);
        
      foreach ($arr as $in_ar)
         $res+=getArrCount($in_ar, $depth-1);
     
      return $res;
  }

example:

$shop = array(array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   )
             );
// call the function

echo getArrCount ($shop);

I hope this will help you.

0

Solved. Thanks guys

function adc($arrayVariable, $counter = 0) {	
if(is_array($arrayVariable)) {		
return adc(current($arrayVariable), $counter + 1);	
} else {		
return $counter;	}
}

Edited by veledrom: .

0

Possible to mark it solved? thnx.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.