array_walk_variation4.phpt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --TEST--
  2. Test array_walk() function : usage variations - 'input' array with subarray
  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. /*
  10. * Testing array_walk() with an array having subarrays as elements
  11. */
  12. echo "*** Testing array_walk() : array with subarray ***\n";
  13. // callback function
  14. /* Prototype : callback(mixed $value, mixed $key)
  15. * Parameters : $value - values in given 'input' array
  16. * $key - keys in given 'input' array
  17. * Description : It prints the count of an array elements, passed as argument
  18. */
  19. function callback($value, $key)
  20. {
  21. // dump the arguments to check that they are passed
  22. // with proper type
  23. var_dump($key); // key
  24. var_dump($value); // value
  25. echo "\n"; // new line to separate the output between each element
  26. }
  27. $input = array(
  28. array(),
  29. array(1),
  30. array(1,2,3),
  31. array("Mango", "Orange"),
  32. array(array(1, 2, 3))
  33. );
  34. var_dump( array_walk( $input, "callback"));
  35. echo "Done"
  36. ?>
  37. --EXPECTF--
  38. *** Testing array_walk() : array with subarray ***
  39. int(0)
  40. array(0) {
  41. }
  42. int(1)
  43. array(1) {
  44. [0]=>
  45. int(1)
  46. }
  47. int(2)
  48. array(3) {
  49. [0]=>
  50. int(1)
  51. [1]=>
  52. int(2)
  53. [2]=>
  54. int(3)
  55. }
  56. int(3)
  57. array(2) {
  58. [0]=>
  59. string(5) "Mango"
  60. [1]=>
  61. string(6) "Orange"
  62. }
  63. int(4)
  64. array(1) {
  65. [0]=>
  66. array(3) {
  67. [0]=>
  68. int(1)
  69. [1]=>
  70. int(2)
  71. [2]=>
  72. int(3)
  73. }
  74. }
  75. bool(true)
  76. Done