Iterator 1 - 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.
Result of php executing
Full code of Iterator 1.php
- <?php
- // Comparison function
- function cmp($a, $b) {
- if ($a == $b) {
- return 0;
- }
- return ($a < $b) ? -1 : 1;
- }
- function str_cmp($a, $b){
- return strcasecmp($a, $b);
- }
- // Array to be sorted
- $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
- $arrayb = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
- $arrayc = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
- $arrayit = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
- // Sort and print the resulting array
- uasort($array, 'cmp');
- print_r($array);
- usort($arrayb, 'cmp');
- print_r($arrayb);
- uksort($arrayc, 'str_cmp');
- print_r($arrayc);
- echo "\n*******************************************************************\n";
- $iterator =
- new RecursiveArrayIterator($arrayit, RecursiveIteratorIterator::LEAVES_ONLY);
- foreach ($iterator as $key => $leaf) {
- echo "$key => $leaf", PHP_EOL;
- }