array_walk_basic2.phpt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. --TEST--
  2. Test array_walk() function : basic functionality - associative 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. // associative array
  11. $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
  12. // User defined callback functions
  13. /* Prototype : test_alter(mixed $item, mixed $key, string $prefix)
  14. * Parameters : item - value in key/value pair
  15. * key - key in key/value pair
  16. * prefix - string to be added
  17. * Description : alters the array values by appending prefix string
  18. */
  19. function test_alter(&$item, $key, $prefix)
  20. {
  21. // dump the arguments to check that they are passed
  22. // with proper type
  23. var_dump($item); // value
  24. var_dump($key); // key
  25. var_dump($prefix); // additional argument passed to callback function
  26. echo "\n"; // new line to separate the output between each element
  27. }
  28. /* Prototype : test_print(mixed $item, mixed $key)
  29. * Parameters : item - value in key/value pair
  30. * key - key in key/value pair
  31. * Description : prints the array values with keys
  32. */
  33. function test_print($item, $key)
  34. {
  35. // dump the arguments to check that they are passed
  36. // with proper type
  37. var_dump($item); // value
  38. var_dump($key); // key
  39. echo "\n"; // new line to separate the output between each element
  40. }
  41. echo "-- Using array_walk with default parameters to show array contents --\n";
  42. var_dump(array_walk($fruits, 'test_print'));
  43. echo "-- Using array_walk with one optional parameter to modify contents --\n";
  44. var_dump (array_walk($fruits, 'test_alter', 'fruit'));
  45. echo "-- Using array_walk with default parameters to show modified array contents --\n";
  46. var_dump (array_walk($fruits, 'test_print'));
  47. echo "Done";
  48. ?>
  49. --EXPECT--
  50. *** Testing array_walk() : basic functionality ***
  51. -- Using array_walk with default parameters to show array contents --
  52. string(5) "lemon"
  53. string(1) "d"
  54. string(6) "orange"
  55. string(1) "a"
  56. string(6) "banana"
  57. string(1) "b"
  58. string(5) "apple"
  59. string(1) "c"
  60. bool(true)
  61. -- Using array_walk with one optional parameter to modify contents --
  62. string(5) "lemon"
  63. string(1) "d"
  64. string(5) "fruit"
  65. string(6) "orange"
  66. string(1) "a"
  67. string(5) "fruit"
  68. string(6) "banana"
  69. string(1) "b"
  70. string(5) "fruit"
  71. string(5) "apple"
  72. string(1) "c"
  73. string(5) "fruit"
  74. bool(true)
  75. -- Using array_walk with default parameters to show modified array contents --
  76. string(5) "lemon"
  77. string(1) "d"
  78. string(6) "orange"
  79. string(1) "a"
  80. string(6) "banana"
  81. string(1) "b"
  82. string(5) "apple"
  83. string(1) "c"
  84. bool(true)
  85. Done