Aantal niveaus tellen van array
Hey,
Het is lang geleden dat ik online ben geweest op phphulp, maar ik heb jullie hulp weer nodig ;)
Ik wil graag het aantal niveaus tellen dat een array heeft. Ik heb dit nodig voor mijn template parser. Is hier een functie voor of is er iemand die een slimme manier heeft om zoiets in elkaar te zetten?
Alvast bedankt allemaal,
Mark
Het is lang geleden dat ik online ben geweest op phphulp, maar ik heb jullie hulp weer nodig ;)
Ik wil graag het aantal niveaus tellen dat een array heeft. Ik heb dit nodig voor mijn template parser. Is hier een functie voor of is er iemand die een slimme manier heeft om zoiets in elkaar te zetten?
Alvast bedankt allemaal,
Mark
Ik had hem zelf geschreven:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
/**
* @author Robert Deiman
* @copyright 2008
* @param array $Array Contains the array to get depth from
* @param integer $DepthCount (used for calculation, do not change!)
*/
function ArrayDepth($Array,$DepthCount=-1) {
// Find maximum depth of an array
// Usage: int ArrayDepth( array $array )
// returns integer with max depth
// if Array is a string or an empty array it will return 0
$DepthArray=array(0);
$DepthCount++;
$Depth = 0;
if (is_array($Array))
foreach ($Array as $Key => $Value) {
$DepthArray[]=ArrayDepth($Value,$DepthCount);
}
else
return $DepthCount;
return max($DepthCount,max($DepthArray));
}
$aTocount = array(
"key1" => "waarde1" ,
"key2" => array(
"subkey1" => "subwaarde1",
"subkey2" => array(
"subsubkey1" => "subsubwaarde1",
"key2" => array(
"subkey1" => "subwaarde1",
"subkey2" => array(
"subsubkey1" => "subsubwaarde1"
)
)
)
)
);
echo ArrayDepth($aTocount);
?>
ini_set('display_errors', 1);
error_reporting(E_ALL);
/**
* @author Robert Deiman
* @copyright 2008
* @param array $Array Contains the array to get depth from
* @param integer $DepthCount (used for calculation, do not change!)
*/
function ArrayDepth($Array,$DepthCount=-1) {
// Find maximum depth of an array
// Usage: int ArrayDepth( array $array )
// returns integer with max depth
// if Array is a string or an empty array it will return 0
$DepthArray=array(0);
$DepthCount++;
$Depth = 0;
if (is_array($Array))
foreach ($Array as $Key => $Value) {
$DepthArray[]=ArrayDepth($Value,$DepthCount);
}
else
return $DepthCount;
return max($DepthCount,max($DepthArray));
}
$aTocount = array(
"key1" => "waarde1" ,
"key2" => array(
"subkey1" => "subwaarde1",
"subkey2" => array(
"subsubkey1" => "subsubwaarde1",
"key2" => array(
"subkey1" => "subwaarde1",
"subkey2" => array(
"subsubkey1" => "subsubwaarde1"
)
)
)
)
);
echo ArrayDepth($aTocount);
?>
Gewijzigd op 01/01/1970 01:00:00 door Robert Deiman
Heb ik geen tijd voor :P
+ ik ben lui :P
+ ik ben lui :P
@Jacco
De vraag was al ooit eerder geweest, als ik interessante vraagstukjes tegenkom hier, wil ik daar wel eens wat voor maken.. :) Zo leer ik ook weer bij..
De vraag was al ooit eerder geweest, als ik interessante vraagstukjes tegenkom hier, wil ik daar wel eens wat voor maken.. :) Zo leer ik ook weer bij..
@Robert
vind het meeste daar niet intressant genoeg voor ;)
vind het meeste daar niet intressant genoeg voor ;)
Neej klopt, maar vond dit toen wel een leuk vraagstuk :) Het zijn ook nog niet veel scriptjes die ik daarom heb geschreven hoor, en kom vooral nu op het werk veel vraagstukken tegen.
Bedankt allebei, hier kan ik wel wat mee :)




