array_map_basic.phpt 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. --TEST--
  2. Test array_map() function : basic functionality
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_map ( callback $callback , array $arr1 [, array $... ] )
  6. * Description: Applies the callback to the elements of the given arrays
  7. * Source code: ext/standard/array.c
  8. */
  9. echo "*** Testing array_map() : basic functionality ***\n";
  10. function multiply($p, $q) {
  11. return ($p * $q);
  12. }
  13. function square($p) {
  14. return ($p * $p);
  15. }
  16. function concatenate($a, $b) {
  17. return "$a = $b";
  18. }
  19. // integer array
  20. $arr1 = array(1, 2, 3);
  21. $arr2 = array(4, 5, 6);
  22. echo "-- With two integer array --\n";
  23. var_dump( array_map('multiply', $arr1, $arr2) );
  24. echo "-- With single integer array --\n";
  25. var_dump( array_map('square', $arr1) );
  26. // string array
  27. $arr1 = array("one", "two");
  28. $arr2 = array("single", "double");
  29. echo "-- With string array --\n";
  30. var_dump( array_map('concatenate', $arr1, $arr2) );
  31. echo "Done";
  32. ?>
  33. --EXPECTF--
  34. *** Testing array_map() : basic functionality ***
  35. -- With two integer array --
  36. array(3) {
  37. [0]=>
  38. int(4)
  39. [1]=>
  40. int(10)
  41. [2]=>
  42. int(18)
  43. }
  44. -- With single integer array --
  45. array(3) {
  46. [0]=>
  47. int(1)
  48. [1]=>
  49. int(4)
  50. [2]=>
  51. int(9)
  52. }
  53. -- With string array --
  54. array(2) {
  55. [0]=>
  56. string(12) "one = single"
  57. [1]=>
  58. string(12) "two = double"
  59. }
  60. Done