note 79749 deleted from ref.array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: sakai dot d4k at gmail dot com 

----

Needed depth of the dimension for arrays might be different by cases, so I made this.  The default of switch might not be 'max' though.

<?php
function array_dimension($array, $option = null) {
	if(!is_array($array)) {
		return 0;
	} elseif(!$array) { // if the element does not exist
		return 1;
	} else {
		switch(strtolower($option)) {
		case 'first':
			$keys = array_keys($array);
			return array_dimension($array[$keys[0]], $option) + 1;
		case 'last':
			$keys = array_keys($array);
			return array_dimension($array[$keys[count($keys) - 1]], $option) + 1;
		case 'min':
			foreach($array as $key => $val) {
				$dems[$key] = array_dimension($val, $option);
			}
			return min($dems) + 1;
		case 'max':
		default:
			foreach($array as $key => $val) {
				$dems[$key] = array_dimension($val, $option);
			}
			return max($dems) + 1;
		}
	}
}
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.