fork download
  1. <?php
  2.  
  3. $categories=array(
  4. array('Code'=>'root','ParentID'=>null),
  5. array('Code'=>'a1','ParentID'=>'root'),
  6. array('Code'=>'a2','ParentID'=>'root'),
  7. array('Code'=>'a1.1','ParentID'=>'a1'),
  8. array('Code'=>'a1.2','ParentID'=>'a1'),
  9. array('Code'=>'a2.1','ParentID'=>'a2'),
  10. );
  11.  
  12. function FoundSub($parent) {
  13. global $categories;
  14. $c=0;
  15.  
  16. foreach( $categories as $key ) {
  17.  
  18. if( $key['ParentID'] == $parent ) {
  19.  
  20. $c+=1+FoundSub($key['Code']);
  21.  
  22. }
  23.  
  24. } // end
  25. return $c;
  26. } // end function
  27.  
  28. echo FoundSub('root')."\n";
  29. echo FoundSub('a1')."\n";
  30. echo FoundSub('a2')."\n";
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
5
2
1