array_walk_basic1.phpt 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --TEST--
  2. Test array_walk() function : basic functionality - regular array
  3. --FILE--
  4. <?php
  5. /* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata])
  6. * Description: Apply a user function to every member of an array
  7. * Source code: ext/standard/array.c
  8. */
  9. echo "*** Testing array_walk() : basic functionality ***\n";
  10. // regular array
  11. $fruits = array("lemon", "orange", "banana", "apple");
  12. /* Prototype : test_print(mixed $item, mixed $key)
  13. * Parameters : item - item in key/item pair
  14. * key - key in key/item pair
  15. * Description : prints the array values with keys
  16. */
  17. function test_print($item, $key)
  18. {
  19. // dump the arguments to check that they are passed
  20. // with proper type
  21. var_dump($item); // value
  22. var_dump($key); // key
  23. echo "\n"; // new line to separate the output between each element
  24. }
  25. function with_userdata($item, $key, $user_data)
  26. {
  27. // dump the arguments to check that they are passed
  28. // with proper type
  29. var_dump($item); // value
  30. var_dump($key); // key
  31. var_dump($user_data); // user supplied data
  32. echo "\n"; // new line to separate the output between each element
  33. }
  34. echo "-- Using array_walk() with default parameters to show array contents --\n";
  35. var_dump( array_walk($fruits, 'test_print'));
  36. echo "-- Using array_walk() with all parameters --\n";
  37. var_dump( array_walk($fruits, 'with_userdata', "Added"));
  38. echo "Done";
  39. ?>
  40. --EXPECT--
  41. *** Testing array_walk() : basic functionality ***
  42. -- Using array_walk() with default parameters to show array contents --
  43. string(5) "lemon"
  44. int(0)
  45. string(6) "orange"
  46. int(1)
  47. string(6) "banana"
  48. int(2)
  49. string(5) "apple"
  50. int(3)
  51. bool(true)
  52. -- Using array_walk() with all parameters --
  53. string(5) "lemon"
  54. int(0)
  55. string(5) "Added"
  56. string(6) "orange"
  57. int(1)
  58. string(5) "Added"
  59. string(6) "banana"
  60. int(2)
  61. string(5) "Added"
  62. string(5) "apple"
  63. int(3)
  64. string(5) "Added"
  65. bool(true)
  66. Done