1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 16:22:37 +01:00
Files
archived-php-src/ext/standard/tests/array/array_merge_recursive_variation4.phpt
Nikita Popov a6c9c7c2b8 Handle resources used as array keys consistently
Resources used as array keys are generally handled by throwing a
notice and converting the resource to the resource handle. The only
exception is the [$resource => null] syntax, where this was treated
as an illegal offset type instead. However, this also only happened
for VM evaluations, the AST evaluator did handle resources correctly.
2019-09-27 10:40:41 +02:00

429 lines
6.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--TEST--
Test array_merge_recursive() function : usage variations - associative array with different keys
--FILE--
<?php
/* Prototype : array array_merge_recursive(array $arr1[, array $...])
* Description: Recursively merges elements from passed arrays into one array
* Source code: ext/standard/array.c
*/
/*
* Testing the functionality of array_merge_recursive() by passing different
* associative arrays having different keys to $arr1 argument.
*/
echo "*** Testing array_merge_recursive() : assoc. array with diff. keys to \$arr1 argument ***\n";
// get an unset variable
$unset_var = 10;
unset ($unset_var);
// get a resource variable
$fp = fopen(__FILE__, "r");
// get a class
class classA
{
public function __toString(){
return "Class A object";
}
}
// get a heredoc string
$heredoc = <<<EOT
Hello world
EOT;
// different associative arrays to be passed to $arr1 argument
$arrays = array (
/*1*/ // arrays with integer keys
array(0 => "0", 1 => array(1 => "one")),
array(1 => "1", 2 => array(1 => "one", 2 => "two", 3 => 1, 4 => "4")),
// arrays with float keys
/*3*/ array(2.3333 => "float", 44.44 => array(1.1 => "float")),
array(1.2 => "f1", 3.33 => "f2", 4.89999922839999 => array(1.1 => "f1"), 3333333.333333 => "f4"),
// arrays with string keys
/*5*/ array('\tHello' => array("hello", 'world'), '\v\fworld' => 2.2, 'pen\n' => 111),
array("\tHello" => array("hello", 'world'), "\v\fworld" => 2.2, "pen\n" => 111),
array("hello", $heredoc => array("heredoc", 'string'), "string"),
// array with object, unset variable and resource variable
/*8*/ array(new classA() => 11, @$unset_var => array("unset"), $fp => 'resource', 11, "hello")
);
// initialise the second array
$arr2 = array( 1 => "one", 2, "string" => "hello", "array" => array("a", "b", "c"));
// loop through each sub array of $arrays and check the behavior of array_merge_recursive()
$iterator = 1;
foreach($arrays as $arr1) {
echo "-- Iteration $iterator --\n";
// with default argument
echo "-- With default argument --\n";
var_dump( array_merge_recursive($arr1) );
// with more arguments
echo "-- With more arguments --\n";
var_dump( array_merge_recursive($arr1, $arr2) );
$iterator++;
}
// close the file resource used
fclose($fp);
echo "Done";
?>
--EXPECTF--
*** Testing array_merge_recursive() : assoc. array with diff. keys to $arr1 argument ***
Warning: Illegal offset type in %s on line %d
Notice: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
-- Iteration 1 --
-- With default argument --
array(2) {
[0]=>
string(1) "0"
[1]=>
array(1) {
[1]=>
string(3) "one"
}
}
-- With more arguments --
array(6) {
[0]=>
string(1) "0"
[1]=>
array(1) {
[1]=>
string(3) "one"
}
[2]=>
string(3) "one"
[3]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 2 --
-- With default argument --
array(2) {
[0]=>
string(1) "1"
[1]=>
array(4) {
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
int(1)
[4]=>
string(1) "4"
}
}
-- With more arguments --
array(6) {
[0]=>
string(1) "1"
[1]=>
array(4) {
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
int(1)
[4]=>
string(1) "4"
}
[2]=>
string(3) "one"
[3]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 3 --
-- With default argument --
array(2) {
[0]=>
string(5) "float"
[1]=>
array(1) {
[1]=>
string(5) "float"
}
}
-- With more arguments --
array(6) {
[0]=>
string(5) "float"
[1]=>
array(1) {
[1]=>
string(5) "float"
}
[2]=>
string(3) "one"
[3]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 4 --
-- With default argument --
array(4) {
[0]=>
string(2) "f1"
[1]=>
string(2) "f2"
[2]=>
array(1) {
[1]=>
string(2) "f1"
}
[3]=>
string(2) "f4"
}
-- With more arguments --
array(8) {
[0]=>
string(2) "f1"
[1]=>
string(2) "f2"
[2]=>
array(1) {
[1]=>
string(2) "f1"
}
[3]=>
string(2) "f4"
[4]=>
string(3) "one"
[5]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 5 --
-- With default argument --
array(3) {
["\tHello"]=>
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
["\v\fworld"]=>
float(2.2)
["pen\n"]=>
int(111)
}
-- With more arguments --
array(7) {
["\tHello"]=>
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
["\v\fworld"]=>
float(2.2)
["pen\n"]=>
int(111)
[0]=>
string(3) "one"
[1]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 6 --
-- With default argument --
array(3) {
[" Hello"]=>
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
[" world"]=>
float(2.2)
["pen
"]=>
int(111)
}
-- With more arguments --
array(7) {
[" Hello"]=>
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
[" world"]=>
float(2.2)
["pen
"]=>
int(111)
[0]=>
string(3) "one"
[1]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 7 --
-- With default argument --
array(3) {
[0]=>
string(5) "hello"
["Hello world"]=>
array(2) {
[0]=>
string(7) "heredoc"
[1]=>
string(6) "string"
}
[1]=>
string(6) "string"
}
-- With more arguments --
array(7) {
[0]=>
string(5) "hello"
["Hello world"]=>
array(2) {
[0]=>
string(7) "heredoc"
[1]=>
string(6) "string"
}
[1]=>
string(6) "string"
[2]=>
string(3) "one"
[3]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
-- Iteration 8 --
-- With default argument --
array(4) {
[""]=>
array(1) {
[0]=>
string(5) "unset"
}
[0]=>
string(8) "resource"
[1]=>
int(11)
[2]=>
string(5) "hello"
}
-- With more arguments --
array(8) {
[""]=>
array(1) {
[0]=>
string(5) "unset"
}
[0]=>
string(8) "resource"
[1]=>
int(11)
[2]=>
string(5) "hello"
[3]=>
string(3) "one"
[4]=>
int(2)
["string"]=>
string(5) "hello"
["array"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
Done