Advertisement
Hiuhu

Recursive functions

Jul 11th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. <?php
  2. function printValues ($arr) {
  3. global $count;
  4. global $out;
  5. if (!is_array($arr)) {
  6. die ('ERROR: Input is not an array');
  7. }
  8. foreach ($arr as $a) {
  9. if (is_array($a)) {
  10. printValues ($a);
  11. } else {
  12. $out [] = $a;
  13. $count++;
  14. }
  15. }
  16. return array ('total' => $count, 'values' => $out);
  17. }
  18. $data = array( 'o' => array('orange','owl','one'), 't' => array('tea', 'ten', 'tag', 'twentythree' => array(array('twenty', 'three'), array('vingt', 'trois', array('red' => 'baron', 'blue' => 'blood')))));
  19. $ret = printValues ($data);
  20. echo $ret['total'] . ' value(s) found: ';
  21. echo implode (',', $ret['values']);
  22. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement