array_walk_variation6.phpt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. --TEST--
  2. Test array_walk() function : usage variations - 'input' argument as diff. associative arrays
  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. * Passing 'input' argument as an associative array
  11. * with Numeric & string keys
  12. */
  13. echo "*** Testing array_walk() : 'input' as an associative array ***\n";
  14. // callback functions
  15. /* Prototype : for_numeric( int $value, int $key, int $user_data)
  16. * Parameters : $value - value from key/value pair of the array
  17. * $key - key from key/value pair of the array
  18. * $user_data - data to be added to 'value'
  19. * Description : Function adds values with keys & user_data
  20. */
  21. function for_numeric($value, $key, $user_data)
  22. {
  23. // dump the input values to see if they are
  24. // passed with correct type
  25. var_dump($key);
  26. var_dump($value);
  27. var_dump($user_data);
  28. echo "\n"; // new line to separate the output between each element
  29. }
  30. /* Prototype : for_string( string $value, string $key)
  31. * Parameters : $value - values in given input array
  32. * $key - keys in given input array
  33. * Description : Function appends key to the value
  34. */
  35. function for_string($value, $key)
  36. {
  37. // dump the input values to see if they are
  38. // passed with correct type
  39. var_dump($key);
  40. var_dump($value);
  41. echo "\n"; // new line to separate the output between each element
  42. }
  43. /* Prototype : for_mixed( mixed $value, mixed $key)
  44. * Parameters : $value - values in given input array
  45. * $key - keys in given input array
  46. * Description : Function displays each element of an array with keys
  47. */
  48. function for_mixed($value, $key)
  49. {
  50. // dump the input values to see if they are
  51. // passed with correct type
  52. var_dump($key);
  53. var_dump($value);
  54. echo "\n"; // new line to separate the output between each element
  55. }
  56. // Numeric keys
  57. $input = array( 1 => 25, 5 => 12, 0 => -80, -2 => 100, 5 => 30);
  58. echo "-- Associative array with numeric keys --\n";
  59. var_dump( array_walk($input, "for_numeric", 10));
  60. // String keys
  61. $input = array( "a" => "Apple", 'b' => 'Bananna', "c" => "carrot", 'o' => "Orange");
  62. echo "-- Associative array with string keys --\n";
  63. var_dump( array_walk($input, "for_string"));
  64. // binary keys
  65. $input = array( b"a" => "Apple", b"b" => "Banana");
  66. echo "-- Associative array with binary keys --\n";
  67. var_dump( array_walk($input, "for_string"));
  68. // Mixed keys - numeric/string
  69. $input = array( 0 => 1, 1 => 2, "a" => "Apple", "b" => "Banana", 2 =>3);
  70. echo "-- Associative array with numeric/string keys --\n";
  71. var_dump( array_walk($input, "for_mixed"));
  72. echo "Done"
  73. ?>
  74. --EXPECT--
  75. *** Testing array_walk() : 'input' as an associative array ***
  76. -- Associative array with numeric keys --
  77. int(1)
  78. int(25)
  79. int(10)
  80. int(5)
  81. int(30)
  82. int(10)
  83. int(0)
  84. int(-80)
  85. int(10)
  86. int(-2)
  87. int(100)
  88. int(10)
  89. bool(true)
  90. -- Associative array with string keys --
  91. string(1) "a"
  92. string(5) "Apple"
  93. string(1) "b"
  94. string(7) "Bananna"
  95. string(1) "c"
  96. string(6) "carrot"
  97. string(1) "o"
  98. string(6) "Orange"
  99. bool(true)
  100. -- Associative array with binary keys --
  101. string(1) "a"
  102. string(5) "Apple"
  103. string(1) "b"
  104. string(6) "Banana"
  105. bool(true)
  106. -- Associative array with numeric/string keys --
  107. int(0)
  108. int(1)
  109. int(1)
  110. int(2)
  111. string(1) "a"
  112. string(5) "Apple"
  113. string(1) "b"
  114. string(6) "Banana"
  115. int(2)
  116. int(3)
  117. bool(true)
  118. Done