Advertisement
westron

Untitled

Dec 3rd, 2020
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     script
  5. */
  6.  
  7. $a = random_array();
  8. $b = array_process($a);
  9.  
  10. print_array($a, 'Array A');
  11. print_array($b, 'Array B');
  12.  
  13.  
  14. /*
  15.     functions
  16. */
  17.  
  18. function array_process($arr) {
  19.     $result = [];
  20.  
  21.     foreach ($arr as $val) {
  22.         if (is_numeric($val) && $val % 2 == 0)
  23.             $result[] = $val;
  24.     }
  25.  
  26.     return $result;
  27. }
  28.  
  29. function random_array() {
  30.     $result = [];
  31.  
  32.     for ($i = 0; $i < rand(10, 20); $i++) {
  33.         $result[] = rand(0, 999);
  34.     }
  35.  
  36.     return $result;
  37. }
  38.  
  39. function print_array($arr, $title = 'Array') {
  40.     printf('<h3>%s</h3>', $title);
  41.     echo '<table border=1>';
  42.     echo '<tr><th>index</th><th>value</th><th>parity</th></tr>';
  43.  
  44.     foreach ($arr as $idx => $val) {
  45.         printf('<tr><td>%d</td><td>%d</td><td>%s</td></tr>', $idx, $val, $val %2 == 0 ? 'even' : 'odd');
  46.     }
  47.  
  48.     echo '</table>';
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement