mirror of
https://github.com/php/php-src.git
synced 2024-09-23 19:07:26 +00:00
New testcases for array_walk_recursive() function
This commit is contained in:
parent
b2def646b6
commit
b1f1baba4d
80
ext/standard/tests/array/array_walk_recursive_basic1.phpt
Normal file
80
ext/standard/tests/array/array_walk_recursive_basic1.phpt
Normal file
@ -0,0 +1,80 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : basic functionality - regular array
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : basic functionality ***\n";
|
||||
|
||||
// regular array
|
||||
$fruits = array("lemon", array("orange", "banana"), array("apple"));
|
||||
|
||||
/* Prototype : test_print(mixed $item, mixed $key)
|
||||
* Parameters : item - item in key/item pair
|
||||
* key - key in key/item pair
|
||||
* Description : prints the array values with keys
|
||||
*/
|
||||
function test_print($item, $key)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($item); // value
|
||||
var_dump($key); // key
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
function with_userdata($item, $key, $user_data)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($item); // value
|
||||
var_dump($key); // key
|
||||
var_dump($user_data); // user supplied data
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
echo "-- Using array_walk_recursive() with default parameters to show array contents --\n";
|
||||
var_dump( array_walk_recursive($fruits, 'test_print'));
|
||||
|
||||
echo "-- Using array_walk_recursive() with all parameters --\n";
|
||||
var_dump( array_walk_recursive($fruits, 'with_userdata', "Added"));
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECT--
|
||||
*** Testing array_walk_recursive() : basic functionality ***
|
||||
-- Using array_walk_recursive() with default parameters to show array contents --
|
||||
string(5) "lemon"
|
||||
int(0)
|
||||
|
||||
string(6) "orange"
|
||||
int(0)
|
||||
|
||||
string(6) "banana"
|
||||
int(1)
|
||||
|
||||
string(5) "apple"
|
||||
int(0)
|
||||
|
||||
bool(true)
|
||||
-- Using array_walk_recursive() with all parameters --
|
||||
string(5) "lemon"
|
||||
int(0)
|
||||
string(5) "Added"
|
||||
|
||||
string(6) "orange"
|
||||
int(0)
|
||||
string(5) "Added"
|
||||
|
||||
string(6) "banana"
|
||||
int(1)
|
||||
string(5) "Added"
|
||||
|
||||
string(5) "apple"
|
||||
int(0)
|
||||
string(5) "Added"
|
||||
|
||||
bool(true)
|
||||
Done
|
105
ext/standard/tests/array/array_walk_recursive_basic2.phpt
Normal file
105
ext/standard/tests/array/array_walk_recursive_basic2.phpt
Normal file
@ -0,0 +1,105 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : basic functionality - associative array
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : basic functionality ***\n";
|
||||
|
||||
// associative array
|
||||
$fruits = array("a" => "lemon", "b" => array( "c" => "orange", "d" => "banana"), "e" => array("f" => "apple"));
|
||||
|
||||
// User defined callback functions
|
||||
/* Prototype : test_alter(mixed $item, mixed $key, string $prefix)
|
||||
* Parameters : item - value in key/value pair
|
||||
* key - key in key/value pair
|
||||
* prefix - string to be added
|
||||
* Description : alters the array values by appending prefix string
|
||||
*/
|
||||
function test_alter(&$item, $key, $prefix)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($item); // value
|
||||
var_dump($key); // key
|
||||
var_dump($prefix); // additional agument passed to callback function
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
/* Prototype : test_print(mixed $item, mixed $key)
|
||||
* Parameters : item - value in key/value pair
|
||||
* key - key in key/value pair
|
||||
* Description : prints the array values with keys
|
||||
*/
|
||||
function test_print($item, $key)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($item); // value
|
||||
var_dump($key); // key
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
echo "-- Using array_walk_recursive with default parameters to show array contents --\n";
|
||||
var_dump(array_walk_recursive($fruits, 'test_print'));
|
||||
|
||||
echo "-- Using array_walk_recursive with one optional parameter to modify contents --\n";
|
||||
var_dump (array_walk_recursive($fruits, 'test_alter', 'fruit'));
|
||||
|
||||
echo "-- Using array_walk_recursive with default parameters to show modified array contents --\n";
|
||||
var_dump (array_walk_recursive($fruits, 'test_print'));
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECT--
|
||||
*** Testing array_walk_recursive() : basic functionality ***
|
||||
-- Using array_walk_recursive with default parameters to show array contents --
|
||||
string(5) "lemon"
|
||||
string(1) "a"
|
||||
|
||||
string(6) "orange"
|
||||
string(1) "c"
|
||||
|
||||
string(6) "banana"
|
||||
string(1) "d"
|
||||
|
||||
string(5) "apple"
|
||||
string(1) "f"
|
||||
|
||||
bool(true)
|
||||
-- Using array_walk_recursive with one optional parameter to modify contents --
|
||||
string(5) "lemon"
|
||||
string(1) "a"
|
||||
string(5) "fruit"
|
||||
|
||||
string(6) "orange"
|
||||
string(1) "c"
|
||||
string(5) "fruit"
|
||||
|
||||
string(6) "banana"
|
||||
string(1) "d"
|
||||
string(5) "fruit"
|
||||
|
||||
string(5) "apple"
|
||||
string(1) "f"
|
||||
string(5) "fruit"
|
||||
|
||||
bool(true)
|
||||
-- Using array_walk_recursive with default parameters to show modified array contents --
|
||||
string(5) "lemon"
|
||||
string(1) "a"
|
||||
|
||||
string(6) "orange"
|
||||
string(1) "c"
|
||||
|
||||
string(6) "banana"
|
||||
string(1) "d"
|
||||
|
||||
string(5) "apple"
|
||||
string(1) "f"
|
||||
|
||||
bool(true)
|
||||
Done
|
49
ext/standard/tests/array/array_walk_recursive_error1.phpt
Normal file
49
ext/standard/tests/array/array_walk_recursive_error1.phpt
Normal file
@ -0,0 +1,49 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : error conditions(Bug#43558)
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
$input = array(1, 2);
|
||||
|
||||
/* Prototype : callback(mixed value, mixed key, mixed user_data)
|
||||
* Parameters : value - value in key/value pair
|
||||
* key - key in key/value pair
|
||||
* user_data - extra parameter
|
||||
*/
|
||||
function callback ($value, $key, $user_data) {
|
||||
echo "\ncallback() invoked \n";
|
||||
}
|
||||
|
||||
echo "*** Testing array_walk_recursive() : error conditions ***\n";
|
||||
|
||||
echo "-- Testing array_walk_recursive() function with zero arguments --\n";
|
||||
var_dump( array_walk_recursive() );
|
||||
|
||||
echo "-- Testing array_walk_recursive() function with one argument --\n";
|
||||
var_dump( array_walk_recursive($input) );
|
||||
|
||||
$input = array( array(1, 2), array(3), array(4, 5));
|
||||
echo "-- Testing array_walk_recursive() function with non existent callback function --\n";
|
||||
var_dump( array_walk_recursive($input, "non_existent") );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : error conditions ***
|
||||
-- Testing array_walk_recursive() function with zero arguments --
|
||||
|
||||
Warning: array_walk_recursive() expects at least 2 parameters, 0 given in %s on line %d
|
||||
NULL
|
||||
-- Testing array_walk_recursive() function with one argument --
|
||||
|
||||
Warning: array_walk_recursive() expects at least 2 parameters, 1 given in %s on line %d
|
||||
NULL
|
||||
-- Testing array_walk_recursive() function with non existent callback function --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d
|
||||
bool(false)
|
||||
Done
|
59
ext/standard/tests/array/array_walk_recursive_error2.phpt
Normal file
59
ext/standard/tests/array/array_walk_recursive_error2.phpt
Normal file
@ -0,0 +1,59 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : error conditions - callback parameters(Bug#43558)
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Testing array_walk_recursive() by passing more number of parameters to callback function
|
||||
*/
|
||||
$input = array(1);
|
||||
|
||||
function callback1($value, $key, $user_data ) {
|
||||
echo "\ncallback1() invoked \n";
|
||||
}
|
||||
|
||||
function callback2($value, $key, $user_data1, $user_data2) {
|
||||
echo "\ncallback2() invoked \n";
|
||||
}
|
||||
echo "*** Testing array_walk_recursive() : error conditions - callback parameters ***\n";
|
||||
|
||||
// expected: Missing argument Warning
|
||||
var_dump( array_walk_recursive($input, "callback1") );
|
||||
var_dump( array_walk_recursive($input, "callback2", 4) );
|
||||
|
||||
// expected: Warning is supressed
|
||||
var_dump( @array_walk_recursive($input, "callback1") );
|
||||
var_dump( @array_walk_recursive($input, "callback2", 4) );
|
||||
|
||||
echo "-- Testing array_walk_recursive() function with too many callback parameters --\n";
|
||||
var_dump( array_walk_recursive($input, "callback1", 20, 10) );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : error conditions - callback parameters ***
|
||||
|
||||
Warning: Missing argument 3 for callback1() in %s on line %d
|
||||
|
||||
callback1() invoked
|
||||
bool(false)
|
||||
|
||||
Warning: Missing argument 4 for callback2() in %s on line %d
|
||||
|
||||
callback2() invoked
|
||||
bool(false)
|
||||
|
||||
callback1() invoked
|
||||
bool(true)
|
||||
|
||||
callback2() invoked
|
||||
bool(true)
|
||||
-- Testing array_walk_recursive() function with too many callback parameters --
|
||||
|
||||
Warning: array_walk_recursive() expects at most 3 parameters, 4 given in %s on line %d
|
||||
NULL
|
||||
Done
|
BIN
ext/standard/tests/array/array_walk_recursive_object1.phpt
Normal file
BIN
ext/standard/tests/array/array_walk_recursive_object1.phpt
Normal file
Binary file not shown.
106
ext/standard/tests/array/array_walk_recursive_object2.phpt
Normal file
106
ext/standard/tests/array/array_walk_recursive_object2.phpt
Normal file
@ -0,0 +1,106 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : object functionality - array of objects
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Testing array_walk_recursive() with an array of objects
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : array of objects ***\n";
|
||||
|
||||
/*
|
||||
* Prototype : callback(mixed $value, mixed $key, int $addvalue
|
||||
* Parameters : $value - values in given input array
|
||||
* $key - keys in given input array
|
||||
* $addvalue - value to be added
|
||||
* Description : Function adds the addvalue to each element of an array
|
||||
*/
|
||||
function callback_private($value, $key, $addValue)
|
||||
{
|
||||
echo "value : ";
|
||||
var_dump($value->getValue());
|
||||
echo "key : ";
|
||||
var_dump($key);
|
||||
}
|
||||
|
||||
function callback_public($value, $key)
|
||||
{
|
||||
echo "value : ";
|
||||
var_dump($value->pub_value);
|
||||
}
|
||||
function callback_protected($value, $key)
|
||||
{
|
||||
echo "value : ";
|
||||
var_dump($value->get_pro_value());
|
||||
}
|
||||
|
||||
class MyClass
|
||||
{
|
||||
private $pri_value;
|
||||
public $pub_value;
|
||||
protected $pro_value;
|
||||
public function __construct($setVal)
|
||||
{
|
||||
$this->pri_value = $setVal;
|
||||
$this->pub_value = $setVal;
|
||||
$this->pro_value = $setVal;
|
||||
}
|
||||
public function getValue()
|
||||
{
|
||||
return $this->pri_value;
|
||||
}
|
||||
public function get_pro_value()
|
||||
{
|
||||
return $this->pro_value;
|
||||
}
|
||||
};
|
||||
|
||||
// array containing objects of MyClass
|
||||
$input = array (
|
||||
array(
|
||||
new MyClass(3),
|
||||
new MyClass(10),
|
||||
),
|
||||
new MyClass(20),
|
||||
array(new MyClass(-10))
|
||||
);
|
||||
|
||||
echo "-- For private member --\n";
|
||||
var_dump( array_walk_recursive($input, "callback_private", 1));
|
||||
echo "-- For public member --\n";
|
||||
var_dump( array_walk_recursive($input, "callback_public"));
|
||||
echo "-- For protected member --\n";
|
||||
var_dump( array_walk_recursive($input, "callback_protected"));
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : array of objects ***
|
||||
-- For private member --
|
||||
value : int(3)
|
||||
key : int(0)
|
||||
value : int(10)
|
||||
key : int(1)
|
||||
value : int(20)
|
||||
key : int(1)
|
||||
value : int(-10)
|
||||
key : int(0)
|
||||
bool(true)
|
||||
-- For public member --
|
||||
value : int(3)
|
||||
value : int(10)
|
||||
value : int(20)
|
||||
value : int(-10)
|
||||
bool(true)
|
||||
-- For protected member --
|
||||
value : int(3)
|
||||
value : int(10)
|
||||
value : int(20)
|
||||
value : int(-10)
|
||||
bool(true)
|
||||
Done
|
250
ext/standard/tests/array/array_walk_recursive_variation1.phpt
Normal file
250
ext/standard/tests/array/array_walk_recursive_variation1.phpt
Normal file
@ -0,0 +1,250 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - unexpected values for 'input' argument
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different scalar/nonscalar values in place of 'input' argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : unexpected values for 'input' argument ***\n";
|
||||
|
||||
// callback function
|
||||
/* Prototype : callback(mixed $value, mixed $key)
|
||||
* Parameters : $value - values given in input array
|
||||
* $key - keys given in input array
|
||||
* Description : Function prints each element of an array with key
|
||||
*/
|
||||
function callback($value, $key)
|
||||
{
|
||||
echo "key : ";
|
||||
var_dump($key);
|
||||
echo "value : ";
|
||||
var_dump($value);
|
||||
}
|
||||
|
||||
// extra parameter passed to array_walk_recursive()
|
||||
$user_data = 10;
|
||||
|
||||
// get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// get resource variable
|
||||
$fp = fopen(__FILE__, 'r');
|
||||
|
||||
// different scalar/nonscalar values to be used in place of an 'input' argument
|
||||
$input_values = array(
|
||||
|
||||
// int data
|
||||
/* 1*/ 0,
|
||||
1,
|
||||
12345,
|
||||
-2345,
|
||||
|
||||
// float data
|
||||
/* 5*/ 10.5,
|
||||
-10.5,
|
||||
10.1234567e8,
|
||||
10.7654321E-8,
|
||||
.5,
|
||||
|
||||
// null data
|
||||
/* 10*/ NULL,
|
||||
null,
|
||||
|
||||
// boolean data
|
||||
/* 12*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// empty data
|
||||
/* 16*/ "",
|
||||
'',
|
||||
|
||||
// string data
|
||||
/* 18*/ "string",
|
||||
'string',
|
||||
|
||||
// resource data
|
||||
$fp,
|
||||
|
||||
// undefined data
|
||||
@$undefined_var,
|
||||
|
||||
// unset data
|
||||
/* 22*/ @$unset_var,
|
||||
);
|
||||
|
||||
|
||||
for($count = 0; $count < count($input_values); $count++) {
|
||||
echo "-- Iteration ".($count + 1)." --\n";
|
||||
var_dump( array_walk_recursive($input_values[$count], "callback") );
|
||||
var_dump( array_walk_recursive($input_values[$count], "callback", $user_data) );
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : unexpected values for 'input' argument ***
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 2 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 5 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 14 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 18 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive(): The argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
Done
|
268
ext/standard/tests/array/array_walk_recursive_variation2.phpt
Normal file
268
ext/standard/tests/array/array_walk_recursive_variation2.phpt
Normal file
@ -0,0 +1,268 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - unexpected values in place of 'funcname' argument(Bug#43558)
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different scalar/nonscalar values in place of 'funcname' argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : unexpected values for 'funcname' argument ***\n";
|
||||
|
||||
$input = array(1, array(2, 3));
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
$user_data = 20;
|
||||
|
||||
// class definition
|
||||
class MyClass
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
return 'object';
|
||||
}
|
||||
}
|
||||
|
||||
// different scalar/nonscalar values to be used in place of callback function
|
||||
$funcname_values = array(
|
||||
|
||||
// int data
|
||||
/* 1*/ 0,
|
||||
1,
|
||||
12345,
|
||||
-2345,
|
||||
|
||||
// float data
|
||||
/* 5*/ 10.5,
|
||||
-10.5,
|
||||
10.1234567e8,
|
||||
10.7654321E-8,
|
||||
.5,
|
||||
|
||||
// array data
|
||||
/* 10*/ array(),
|
||||
array(0),
|
||||
array(1),
|
||||
array('color' => 'red', 'item' => 'pen'),
|
||||
|
||||
// null data
|
||||
/* 14*/ NULL,
|
||||
null,
|
||||
|
||||
// boolean data
|
||||
/* 16*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// empty data
|
||||
/* 20*/ "",
|
||||
'',
|
||||
|
||||
// object data
|
||||
new MyClass(),
|
||||
|
||||
// resource data
|
||||
/* 23*/ $fp = fopen(__FILE__, 'r'),
|
||||
|
||||
// undefined data
|
||||
@$undefined_var,
|
||||
|
||||
// unset data
|
||||
/* 25*/ @$unset_var,
|
||||
);
|
||||
|
||||
for($count = 0; $count < count($funcname_values); $count++) {
|
||||
echo "-- Iteration ".($count + 1)." --\n";
|
||||
var_dump( array_walk_recursive($input, $funcname_values[$count]) );
|
||||
var_dump( array_walk_recursive($input, $funcname_values[$count], $user_data ));
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : unexpected values for 'funcname' argument ***
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 2 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 5 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, double given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, array given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 14 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 18 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, boolean given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, object given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, object given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 23 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, resource given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, resource given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 24 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
-- Iteration 25 --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, null given in %s on line %d
|
||||
bool(false)
|
||||
Done
|
123
ext/standard/tests/array/array_walk_recursive_variation3.phpt
Normal file
123
ext/standard/tests/array/array_walk_recursive_variation3.phpt
Normal file
@ -0,0 +1,123 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - 'input' array with different values
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Testing array_walk_recursive() with following types of 'input' arrays:
|
||||
* integer, float, string, bool, null, empty & mixed
|
||||
*/
|
||||
|
||||
// callback function
|
||||
/*
|
||||
* Prototype : print_value(mixed $value, int $key, int $count)
|
||||
* Parameters : $value - array entries(values)
|
||||
* $key - keys in given input array
|
||||
* $count - extra parameter used as an index
|
||||
* Description : prints the array values with keys and count value
|
||||
*/
|
||||
function print_value($value, $key, $count)
|
||||
{
|
||||
echo $count." : ".$key." ".$value."\n";
|
||||
}
|
||||
|
||||
echo "*** Testing array_walk_recursive() : 'input' array with different values***\n";
|
||||
|
||||
// different arrays as input
|
||||
$input_values = array(
|
||||
|
||||
// integer values
|
||||
/*1*/ array(array(1, 0, -10), array(023, -041), array(0x5A, 0X1F, -0x6E)),
|
||||
|
||||
// float value
|
||||
array(array(3.4, 0.8, -2.9), array(6.25e2, 8.20E-3)),
|
||||
|
||||
// string values
|
||||
array('Mango', array("Apple", 'Orange', "Lemon")),
|
||||
|
||||
// bool values
|
||||
/*4*/ array( array(true, false), array(TRUE, FALSE)),
|
||||
|
||||
// null values
|
||||
array( array(null), array(NULL)),
|
||||
|
||||
// empty array
|
||||
array(),
|
||||
|
||||
// binary array
|
||||
array(array(b'binary')),
|
||||
|
||||
// mixed array
|
||||
/*8*/ array(16, 8.345, array("Fruits"), array(true, null), array(FALSE), array(-98, 0.005, 'banana'))
|
||||
);
|
||||
|
||||
for($count = 0; $count < count($input_values); $count++) {
|
||||
echo "\n-- Iteration ".($count + 1)." --\n";
|
||||
var_dump( array_walk_recursive($input_values[$count], "print_value", $count+1));
|
||||
}
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : 'input' array with different values***
|
||||
|
||||
-- Iteration 1 --
|
||||
1 : 0 1
|
||||
1 : 1 0
|
||||
1 : 2 -10
|
||||
1 : 0 19
|
||||
1 : 1 -33
|
||||
1 : 0 90
|
||||
1 : 1 31
|
||||
1 : 2 -110
|
||||
bool(true)
|
||||
|
||||
-- Iteration 2 --
|
||||
2 : 0 3.4
|
||||
2 : 1 0.8
|
||||
2 : 2 -2.9
|
||||
2 : 0 625
|
||||
2 : 1 0.0082
|
||||
bool(true)
|
||||
|
||||
-- Iteration 3 --
|
||||
3 : 0 Mango
|
||||
3 : 0 Apple
|
||||
3 : 1 Orange
|
||||
3 : 2 Lemon
|
||||
bool(true)
|
||||
|
||||
-- Iteration 4 --
|
||||
4 : 0 1
|
||||
4 : 1
|
||||
4 : 0 1
|
||||
4 : 1
|
||||
bool(true)
|
||||
|
||||
-- Iteration 5 --
|
||||
5 : 0
|
||||
5 : 0
|
||||
bool(true)
|
||||
|
||||
-- Iteration 6 --
|
||||
bool(true)
|
||||
|
||||
-- Iteration 7 --
|
||||
7 : 0 binary
|
||||
bool(true)
|
||||
|
||||
-- Iteration 8 --
|
||||
8 : 0 16
|
||||
8 : 1 8.345
|
||||
8 : 0 Fruits
|
||||
8 : 0 1
|
||||
8 : 1
|
||||
8 : 0
|
||||
8 : 0 -98
|
||||
8 : 1 0.005
|
||||
8 : 2 banana
|
||||
bool(true)
|
||||
Done
|
@ -0,0 +1,76 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - 'input' array with subarray
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Testing array_walk_recursive() with an array having subarrays as elements
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : array with subarray ***\n";
|
||||
|
||||
// callback function
|
||||
/* Prototype : callback(mixed $value, mixed $key)
|
||||
* Parameters : $value - values in given 'input' array
|
||||
* $key - keys in given 'input' array
|
||||
* Description : It prints the count of an array elements, passed as argument
|
||||
*/
|
||||
function callback($value, $key)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($key); // key
|
||||
var_dump($value); // value
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
$input = array(
|
||||
array(),
|
||||
array(1),
|
||||
array(1,2,3),
|
||||
array("Mango", "Orange"),
|
||||
array(array(1, 2, 3), array(1))
|
||||
);
|
||||
|
||||
var_dump( array_walk_recursive( $input, "callback"));
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : array with subarray ***
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
int(1)
|
||||
int(2)
|
||||
|
||||
int(2)
|
||||
int(3)
|
||||
|
||||
int(0)
|
||||
string(5) "Mango"
|
||||
|
||||
int(1)
|
||||
string(6) "Orange"
|
||||
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
int(1)
|
||||
int(2)
|
||||
|
||||
int(2)
|
||||
int(3)
|
||||
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
bool(true)
|
||||
Done
|
@ -0,0 +1,64 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - 'input' argument containing reference variables
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Testing array_walk_recursive() with an array having reference variables
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : array with references ***\n";
|
||||
|
||||
$value1 = 10;
|
||||
$value2 = -20;
|
||||
$value3 = &$value1;
|
||||
$value4 = 50;
|
||||
|
||||
// 'input' array containing references to above variables
|
||||
$input = array(&$value1, array(&$value2, -35), array(&$value3, 0), array(&$value4));
|
||||
|
||||
// callback function
|
||||
/* Prototype : callback(int $value, mixed $key)
|
||||
* Parameters : $value - values in given input array
|
||||
* $key - keys in given input array
|
||||
* Description : function checks for the value whether positive or negative and displays according to that
|
||||
*/
|
||||
function callback($value, $key)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($key); // key
|
||||
var_dump($value); // value
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
var_dump( array_walk_recursive($input, "callback"));
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : array with references ***
|
||||
int(0)
|
||||
int(10)
|
||||
|
||||
int(0)
|
||||
int(-20)
|
||||
|
||||
int(1)
|
||||
int(-35)
|
||||
|
||||
int(0)
|
||||
int(10)
|
||||
|
||||
int(1)
|
||||
int(0)
|
||||
|
||||
int(0)
|
||||
int(50)
|
||||
|
||||
bool(true)
|
||||
Done
|
147
ext/standard/tests/array/array_walk_recursive_variation6.phpt
Normal file
147
ext/standard/tests/array/array_walk_recursive_variation6.phpt
Normal file
@ -0,0 +1,147 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - 'input' argument as diff. associative arrays
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing 'input' argument as an associative array
|
||||
* with Numeric & string keys
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : 'input' as an associative array ***\n";
|
||||
|
||||
// callback functions
|
||||
/* Prototype : for_numeric( int $value, int $key, int $user_data)
|
||||
* Parameters : $value - value from key/value pair of the array
|
||||
* $key - key from key/value pair of the array
|
||||
* $user_data - data to be added to 'value'
|
||||
* Description : Function adds values with keys & user_data
|
||||
*/
|
||||
function for_numeric($value, $key, $user_data)
|
||||
{
|
||||
// dump the input values to see if they are
|
||||
// passed with correct type
|
||||
var_dump($key);
|
||||
var_dump($value);
|
||||
var_dump($user_data);
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
/* Prototype : for_string( string $value, string $key)
|
||||
* Parameters : $value - values in given input array
|
||||
* $key - keys in given input array
|
||||
* Description : Function appends key to the value
|
||||
*/
|
||||
function for_string($value, $key)
|
||||
{
|
||||
// dump the input values to see if they are
|
||||
// passed with correct type
|
||||
var_dump($key);
|
||||
var_dump($value);
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
/* Prototype : for_mixed( mixed $value, mixed $key)
|
||||
* Parameters : $value - values in given input array
|
||||
* $key - keys in given input array
|
||||
* Description : Function displays each element of an array with keys
|
||||
*/
|
||||
function for_mixed($value, $key)
|
||||
{
|
||||
// dump the input values to see if they are
|
||||
// passed with correct type
|
||||
var_dump($key);
|
||||
var_dump($value);
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
|
||||
// Numeric keys
|
||||
$input = array( 0 => array(1 => 25, 5 => 12, 0 => -80), 1 => array(-2 => 100, 5 => 30));
|
||||
echo "-- Associative array with numeric keys --\n";
|
||||
var_dump( array_walk_recursive($input, "for_numeric", 10));
|
||||
|
||||
// String keys
|
||||
$input = array( "a" => "Apple", 'z' => array('b' => 'Bananna', "c" => "carrot"), 'x' => array('o' => "Orange"));
|
||||
echo "-- Associative array with string keys --\n";
|
||||
var_dump( array_walk_recursive($input, "for_string"));
|
||||
|
||||
// binary key
|
||||
$input = array( b"a" => "Apple", b"b" => "Banana");
|
||||
echo "-- Associative array with binary keys --\n";
|
||||
var_dump( array_walk_recursive($input, "for_string"));
|
||||
|
||||
// Mixed keys - numeric/string
|
||||
$input = array( 0 => array(0 => 1, 1 => 2), "x" => array("a" => "Apple", "b" => "Banana"), 2 =>3);
|
||||
echo "-- Associative array with numeric/string keys --\n";
|
||||
var_dump( array_walk_recursive($input, "for_mixed"));
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : 'input' as an associative array ***
|
||||
-- Associative array with numeric keys --
|
||||
int(1)
|
||||
int(25)
|
||||
int(10)
|
||||
|
||||
int(5)
|
||||
int(12)
|
||||
int(10)
|
||||
|
||||
int(0)
|
||||
int(-80)
|
||||
int(10)
|
||||
|
||||
int(-2)
|
||||
int(100)
|
||||
int(10)
|
||||
|
||||
int(5)
|
||||
int(30)
|
||||
int(10)
|
||||
|
||||
bool(true)
|
||||
-- Associative array with string keys --
|
||||
string(1) "a"
|
||||
string(5) "Apple"
|
||||
|
||||
string(1) "b"
|
||||
string(7) "Bananna"
|
||||
|
||||
string(1) "c"
|
||||
string(6) "carrot"
|
||||
|
||||
string(1) "o"
|
||||
string(6) "Orange"
|
||||
|
||||
bool(true)
|
||||
-- Associative array with binary keys --
|
||||
string(1) "a"
|
||||
string(5) "Apple"
|
||||
|
||||
string(1) "b"
|
||||
string(6) "Banana"
|
||||
|
||||
bool(true)
|
||||
-- Associative array with numeric/string keys --
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
int(1)
|
||||
int(2)
|
||||
|
||||
string(1) "a"
|
||||
string(5) "Apple"
|
||||
|
||||
string(1) "b"
|
||||
string(6) "Banana"
|
||||
|
||||
int(2)
|
||||
int(3)
|
||||
|
||||
bool(true)
|
||||
Done
|
@ -0,0 +1,93 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - anonymous callback function
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : proto bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing anonymous(run-time) callback function with following variations:
|
||||
* with one parameter
|
||||
* two parameters
|
||||
* three parameters
|
||||
* extra parameters
|
||||
* without parameters
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : anonymous function as callback ***\n";
|
||||
|
||||
$input = array( array(2, 5), array(10, 0));
|
||||
|
||||
echo "-- Anonymous function with one argument --\n";
|
||||
var_dump( array_walk_recursive($input, create_function('$value', 'var_dump($value); echo "\n";')));
|
||||
|
||||
echo "-- Anonymous function with two arguments --\n";
|
||||
var_dump( array_walk_recursive($input, create_function('$value, $key', 'var_dump($key); var_dump($value); echo "\n";')));
|
||||
|
||||
echo "-- Anonymous function with three arguments --\n";
|
||||
var_dump( array_walk_recursive($input, create_function('$value, $key, $user_data', 'var_dump($key); var_dump($value); var_dump($user_data); echo "\n";'), 10));
|
||||
|
||||
echo "-- Anonymous function with one more argument --\n";
|
||||
var_dump( array_walk_recursive($input, create_function('$value, $key, $user_data', 'var_dump($key); var_dump($value); var_dump($user_data); echo "\n";'), 20, 30));
|
||||
|
||||
echo "-- Anonymous function with null argument --\n";
|
||||
var_dump( array_walk_recursive( $input, create_function(null, 'echo "1\n";')));
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : anonymous function as callback ***
|
||||
-- Anonymous function with one argument --
|
||||
int(2)
|
||||
|
||||
int(5)
|
||||
|
||||
int(10)
|
||||
|
||||
int(0)
|
||||
|
||||
bool(true)
|
||||
-- Anonymous function with two arguments --
|
||||
int(0)
|
||||
int(2)
|
||||
|
||||
int(1)
|
||||
int(5)
|
||||
|
||||
int(0)
|
||||
int(10)
|
||||
|
||||
int(1)
|
||||
int(0)
|
||||
|
||||
bool(true)
|
||||
-- Anonymous function with three arguments --
|
||||
int(0)
|
||||
int(2)
|
||||
int(10)
|
||||
|
||||
int(1)
|
||||
int(5)
|
||||
int(10)
|
||||
|
||||
int(0)
|
||||
int(10)
|
||||
int(10)
|
||||
|
||||
int(1)
|
||||
int(0)
|
||||
int(10)
|
||||
|
||||
bool(true)
|
||||
-- Anonymous function with one more argument --
|
||||
|
||||
Warning: array_walk_recursive() expects at most 3 parameters, 4 given in %s on line %d
|
||||
NULL
|
||||
-- Anonymous function with null argument --
|
||||
1
|
||||
1
|
||||
1
|
||||
1
|
||||
bool(true)
|
||||
Done
|
@ -0,0 +1,42 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - buit-in function as callback(Bug#43558)
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different buit-in functionns as callback function
|
||||
* pow function
|
||||
* min function
|
||||
* echo language construct
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : built-in function as callback ***\n";
|
||||
|
||||
$input = array(array(2 => 1, 65), array(98, 100), array(6 => -4));
|
||||
|
||||
echo "-- With 'pow' built-in function --\n";
|
||||
var_dump( array_walk_recursive($input, 'pow'));
|
||||
|
||||
echo "-- With 'min' built-in function --\n";
|
||||
var_dump( array_walk_recursive($input, "min"));
|
||||
|
||||
echo "-- With 'echo' language construct --\n";
|
||||
var_dump( array_walk_recursive($input, "echo"));
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : built-in function as callback ***
|
||||
-- With 'pow' built-in function --
|
||||
bool(true)
|
||||
-- With 'min' built-in function --
|
||||
bool(true)
|
||||
-- With 'echo' language construct --
|
||||
|
||||
Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d
|
||||
bool(false)
|
||||
Done
|
@ -0,0 +1,99 @@
|
||||
--TEST--
|
||||
Test array_walk_recursive() function : usage variations - different callback functions
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
|
||||
* Description: Apply a user function to every member of an array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different types of callback functions to array_walk_recursive()
|
||||
* without parameters
|
||||
* with less and more parameters
|
||||
*/
|
||||
|
||||
echo "*** Testing array_walk_recursive() : callback function variation ***\n";
|
||||
|
||||
$input = array(array('Apple', 'Banana'), 'Mango', array('Orange'));
|
||||
|
||||
echo "-- callback function with both parameters --\n";
|
||||
function callback_two_parameter($value, $key)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($key); // key
|
||||
var_dump($value); // value
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
var_dump( array_walk_recursive($input, 'callback_two_parameter'));
|
||||
|
||||
echo "-- callback function with only one parameter --\n";
|
||||
function callback_one_parameter($value)
|
||||
{
|
||||
// dump the arguments to check that they are passed
|
||||
// with proper type
|
||||
var_dump($value); // value
|
||||
echo "\n"; // new line to separate the output between each element
|
||||
}
|
||||
var_dump( array_walk_recursive($input, 'callback_one_parameter'));
|
||||
|
||||
echo "-- callback function without parameters --\n";
|
||||
function callback_no_parameter()
|
||||
{
|
||||
echo "callback3() called\n";
|
||||
}
|
||||
var_dump( array_walk_recursive($input, 'callback_no_parameter'));
|
||||
|
||||
echo "-- passing one more parameter to function with two parameters --\n";
|
||||
var_dump( array_walk_recursive($input, 'callback_two_parameter', 10));
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_walk_recursive() : callback function variation ***
|
||||
-- callback function with both parameters --
|
||||
int(0)
|
||||
string(5) "Apple"
|
||||
|
||||
int(1)
|
||||
string(6) "Banana"
|
||||
|
||||
int(1)
|
||||
string(5) "Mango"
|
||||
|
||||
int(0)
|
||||
string(6) "Orange"
|
||||
|
||||
bool(true)
|
||||
-- callback function with only one parameter --
|
||||
string(5) "Apple"
|
||||
|
||||
string(6) "Banana"
|
||||
|
||||
string(5) "Mango"
|
||||
|
||||
string(6) "Orange"
|
||||
|
||||
bool(true)
|
||||
-- callback function without parameters --
|
||||
callback3() called
|
||||
callback3() called
|
||||
callback3() called
|
||||
callback3() called
|
||||
bool(true)
|
||||
-- passing one more parameter to function with two parameters --
|
||||
int(0)
|
||||
string(5) "Apple"
|
||||
|
||||
int(1)
|
||||
string(6) "Banana"
|
||||
|
||||
int(1)
|
||||
string(5) "Mango"
|
||||
|
||||
int(0)
|
||||
string(6) "Orange"
|
||||
|
||||
bool(true)
|
||||
Done
|
Loading…
Reference in New Issue
Block a user