1b2def646SRaghubansh Kumar--TEST--
2b2def646SRaghubansh KumarTest array_walk() function : basic functionality - associative array
3b2def646SRaghubansh Kumar--FILE--
4b2def646SRaghubansh Kumar<?php
5b2def646SRaghubansh Kumar/* Prototype  : bool array_walk(array $input, string $funcname [, mixed $userdata])
6782352c5SPeter Kokot * Description: Apply a user function to every member of an array
7b2def646SRaghubansh Kumar * Source code: ext/standard/array.c
8b2def646SRaghubansh Kumar*/
9b2def646SRaghubansh Kumar
10b2def646SRaghubansh Kumarecho "*** Testing array_walk() : basic functionality ***\n";
11b2def646SRaghubansh Kumar
12b2def646SRaghubansh Kumar// associative array
13b2def646SRaghubansh Kumar$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
14b2def646SRaghubansh Kumar
15b2def646SRaghubansh Kumar// User defined callback functions
16b2def646SRaghubansh Kumar/*  Prototype : test_alter(mixed $item, mixed $key, string $prefix)
17782352c5SPeter Kokot *  Parameters : item - value in key/value pair
18b2def646SRaghubansh Kumar *               key - key in key/value pair
19782352c5SPeter Kokot *               prefix - string to be added
20b2def646SRaghubansh Kumar *  Description : alters the array values by appending prefix string
21782352c5SPeter Kokot */
22b2def646SRaghubansh Kumarfunction test_alter(&$item, $key, $prefix)
23b2def646SRaghubansh Kumar{
24b2def646SRaghubansh Kumar  // dump the arguments to check that they are passed
25b2def646SRaghubansh Kumar  // with proper type
26b2def646SRaghubansh Kumar  var_dump($item); // value
27b2def646SRaghubansh Kumar  var_dump($key);  // key
2804145dc2SVeres Lajos  var_dump($prefix); // additional argument passed to callback function
29b2def646SRaghubansh Kumar  echo "\n"; // new line to separate the output between each element
30b2def646SRaghubansh Kumar}
31b2def646SRaghubansh Kumar
32b2def646SRaghubansh Kumar/*  Prototype : test_print(mixed $item, mixed $key)
33782352c5SPeter Kokot *  Parameters : item - value in key/value pair
34b2def646SRaghubansh Kumar *               key - key in key/value pair
35b2def646SRaghubansh Kumar *  Description : prints the array values with keys
36b2def646SRaghubansh Kumar */
37b2def646SRaghubansh Kumarfunction test_print($item, $key)
38b2def646SRaghubansh Kumar{
39b2def646SRaghubansh Kumar  // dump the arguments to check that they are passed
40b2def646SRaghubansh Kumar  // with proper type
41b2def646SRaghubansh Kumar  var_dump($item); // value
42b2def646SRaghubansh Kumar  var_dump($key);  // key
43b2def646SRaghubansh Kumar  echo "\n"; // new line to separate the output between each element
44b2def646SRaghubansh Kumar}
45b2def646SRaghubansh Kumar
46b2def646SRaghubansh Kumarecho "-- Using array_walk with default parameters to show array contents --\n";
47b2def646SRaghubansh Kumarvar_dump(array_walk($fruits, 'test_print'));
48b2def646SRaghubansh Kumar
49b2def646SRaghubansh Kumarecho "-- Using array_walk with one optional parameter to modify contents --\n";
50b2def646SRaghubansh Kumarvar_dump (array_walk($fruits, 'test_alter', 'fruit'));
51b2def646SRaghubansh Kumar
52b2def646SRaghubansh Kumarecho "-- Using array_walk with default parameters to show modified array contents --\n";
53b2def646SRaghubansh Kumarvar_dump (array_walk($fruits, 'test_print'));
54b2def646SRaghubansh Kumar
55b2def646SRaghubansh Kumarecho "Done";
56b2def646SRaghubansh Kumar?>
57b2def646SRaghubansh Kumar--EXPECT--
58b2def646SRaghubansh Kumar*** Testing array_walk() : basic functionality ***
59b2def646SRaghubansh Kumar-- Using array_walk with default parameters to show array contents --
60b2def646SRaghubansh Kumarstring(5) "lemon"
61b2def646SRaghubansh Kumarstring(1) "d"
62b2def646SRaghubansh Kumar
63b2def646SRaghubansh Kumarstring(6) "orange"
64b2def646SRaghubansh Kumarstring(1) "a"
65b2def646SRaghubansh Kumar
66b2def646SRaghubansh Kumarstring(6) "banana"
67b2def646SRaghubansh Kumarstring(1) "b"
68b2def646SRaghubansh Kumar
69b2def646SRaghubansh Kumarstring(5) "apple"
70b2def646SRaghubansh Kumarstring(1) "c"
71b2def646SRaghubansh Kumar
72b2def646SRaghubansh Kumarbool(true)
73b2def646SRaghubansh Kumar-- Using array_walk with one optional parameter to modify contents --
74b2def646SRaghubansh Kumarstring(5) "lemon"
75b2def646SRaghubansh Kumarstring(1) "d"
76b2def646SRaghubansh Kumarstring(5) "fruit"
77b2def646SRaghubansh Kumar
78b2def646SRaghubansh Kumarstring(6) "orange"
79b2def646SRaghubansh Kumarstring(1) "a"
80b2def646SRaghubansh Kumarstring(5) "fruit"
81b2def646SRaghubansh Kumar
82b2def646SRaghubansh Kumarstring(6) "banana"
83b2def646SRaghubansh Kumarstring(1) "b"
84b2def646SRaghubansh Kumarstring(5) "fruit"
85b2def646SRaghubansh Kumar
86b2def646SRaghubansh Kumarstring(5) "apple"
87b2def646SRaghubansh Kumarstring(1) "c"
88b2def646SRaghubansh Kumarstring(5) "fruit"
89b2def646SRaghubansh Kumar
90b2def646SRaghubansh Kumarbool(true)
91b2def646SRaghubansh Kumar-- Using array_walk with default parameters to show modified array contents --
92b2def646SRaghubansh Kumarstring(5) "lemon"
93b2def646SRaghubansh Kumarstring(1) "d"
94b2def646SRaghubansh Kumar
95b2def646SRaghubansh Kumarstring(6) "orange"
96b2def646SRaghubansh Kumarstring(1) "a"
97b2def646SRaghubansh Kumar
98b2def646SRaghubansh Kumarstring(6) "banana"
99b2def646SRaghubansh Kumarstring(1) "b"
100b2def646SRaghubansh Kumar
101b2def646SRaghubansh Kumarstring(5) "apple"
102b2def646SRaghubansh Kumarstring(1) "c"
103b2def646SRaghubansh Kumar
104b2def646SRaghubansh Kumarbool(true)
105b2def646SRaghubansh KumarDone
106