Iterator 2 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Iterator 2 .php

  1. <?php
  2. // Comparison function
  3. function cmp($a, $b) {
  4.     if ($a == $b) {
  5.         return 0;
  6.     }
  7.     return ($a < $b) ? -1 : 1;
  8. }
  9.  
  10. function str_cmp($a, $b){
  11.     
  12.     return strcasecmp($a, $b);
  13. }
  14.  
  15. // Array to be sorted
  16. $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
  17.  
  18. $arrayb = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
  19.  
  20. $arrayc = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
  21.  
  22. $arrayit = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4, array('i' => 100, 'm' => -1));
  23.  
  24. // Sort and print the resulting array
  25. uasort($array, 'cmp');
  26. print_r($array);
  27.  
  28. usort($arrayb, 'cmp');
  29. print_r($arrayb);
  30.  
  31. uksort($arrayc, 'str_cmp');
  32. print_r($arrayc);
  33.  
  34. echo "\n*******************************************************************\n";
  35.  
  36. $iterator = 
  37.     new RecursiveArrayIterator($arrayit, RecursiveIteratorIterator::LEAVES_ONLY);
  38.     
  39.  
  40. foreach ($iterator as $key => $leaf) {
  41.     echo "$key => $leaf", PHP_EOL;
  42. }
  43.  
  44.  
  45.  
File Description
  • Iterator 2
  • PHP Code
  • 18 Mar-2021
  • 1.05 Kb
You can Share it: