mirror of
https://github.com/php/php-src.git
synced 2026-04-26 17:38:14 +02:00
- New tests for array_shift() function
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
--TEST--
|
||||
Test array_shift() function : basic functionality
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test basic functionality of array_shift()
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : basic functionality ***\n";
|
||||
|
||||
$array = array('zero', 'one', '3' => 'three', 'four' => 4);
|
||||
echo "\n-- Before shift: --\n";
|
||||
var_dump($array);
|
||||
|
||||
echo "\n-- After shift: --\n";
|
||||
echo "Returned value:\t";
|
||||
var_dump(array_shift($array));
|
||||
echo "New array:\n";
|
||||
var_dump($array);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : basic functionality ***
|
||||
|
||||
-- Before shift: --
|
||||
array(4) {
|
||||
[0]=>
|
||||
string(4) "zero"
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[3]=>
|
||||
string(5) "three"
|
||||
["four"]=>
|
||||
int(4)
|
||||
}
|
||||
|
||||
-- After shift: --
|
||||
Returned value: string(4) "zero"
|
||||
New array:
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(5) "three"
|
||||
["four"]=>
|
||||
int(4)
|
||||
}
|
||||
Done
|
||||
@@ -0,0 +1,40 @@
|
||||
--TEST--
|
||||
Test array_shift() function : error conditions - Pass incorrect number of args
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass incorrect number of arguments to array_shift() to test behaviour
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : error conditions ***\n";
|
||||
|
||||
// Zero arguments
|
||||
echo "\n-- Testing array_shift() function with Zero arguments --\n";
|
||||
var_dump( array_shift() );
|
||||
|
||||
//Test array_shift with one more than the expected number of arguments
|
||||
echo "\n-- Testing array_shift() function with more than expected no. of arguments --\n";
|
||||
$stack = array(1, 2);
|
||||
$extra_arg = 10;
|
||||
var_dump( array_shift($stack, $extra_arg) );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : error conditions ***
|
||||
|
||||
-- Testing array_shift() function with Zero arguments --
|
||||
|
||||
Warning: array_shift() expects exactly 1 parameter, 0 given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Testing array_shift() function with more than expected no. of arguments --
|
||||
|
||||
Warning: array_shift() expects exactly 1 parameter, 2 given in %s on line %d
|
||||
NULL
|
||||
Done
|
||||
@@ -0,0 +1,218 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - Pass different data types as $stack arg
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass different data types as $stack argument to array_shift() to test behaviour
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// get a class
|
||||
class classA
|
||||
{
|
||||
public function __toString() {
|
||||
return "Class A object";
|
||||
}
|
||||
}
|
||||
|
||||
// heredoc string
|
||||
$heredoc = <<<EOT
|
||||
hello world
|
||||
EOT;
|
||||
|
||||
// get a resource variable
|
||||
$fp = fopen(__FILE__, "r");
|
||||
|
||||
// unexpected values to be passed to $stack argument
|
||||
$inputs = array(
|
||||
|
||||
// int data
|
||||
/*1*/ 0,
|
||||
1,
|
||||
12345,
|
||||
-2345,
|
||||
|
||||
// float data
|
||||
/*5*/ 10.5,
|
||||
-10.5,
|
||||
12.3456789000e10,
|
||||
12.3456789000E-10,
|
||||
.5,
|
||||
|
||||
// null data
|
||||
/*10*/ NULL,
|
||||
null,
|
||||
|
||||
// boolean data
|
||||
/*12*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// empty data
|
||||
/*16*/ "",
|
||||
'',
|
||||
|
||||
// string data
|
||||
/*18*/ "string",
|
||||
'string',
|
||||
$heredoc,
|
||||
|
||||
// object data
|
||||
/*21*/ new classA(),
|
||||
|
||||
// undefined data
|
||||
/*22*/ @$undefined_var,
|
||||
|
||||
// unset data
|
||||
/*23*/ @$unset_var,
|
||||
|
||||
// resource variable
|
||||
/*24*/ $fp
|
||||
);
|
||||
|
||||
// loop through each element of $inputs to check the behavior of array_shift()
|
||||
$iterator = 1;
|
||||
foreach($inputs as $input) {
|
||||
echo "\n-- Iteration $iterator --\n";
|
||||
var_dump( array_shift($input) );
|
||||
$iterator++;
|
||||
};
|
||||
|
||||
fclose($fp);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, integer given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 2 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, integer given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, integer given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, integer given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 5 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, double given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, double given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, double given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, double given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, double given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, null given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, null given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, boolean given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, boolean given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 14 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, boolean given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, boolean given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 18 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, object given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, null given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 23 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, null given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 24 --
|
||||
|
||||
Warning: array_shift() expects parameter 1 to be array, resource given in %s on line 85
|
||||
NULL
|
||||
Done
|
||||
@@ -0,0 +1,208 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - Pass arrays of different data types
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass arrays where values are of one data type to test behaviour of array_shift()
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// get a class
|
||||
class classA
|
||||
{
|
||||
public function __toString() {
|
||||
return "Class A object";
|
||||
}
|
||||
}
|
||||
|
||||
// heredoc string
|
||||
$heredoc = <<<EOT
|
||||
hello world
|
||||
EOT;
|
||||
|
||||
// get a resource variable
|
||||
$fp = fopen(__FILE__, "r");
|
||||
|
||||
// arrays of different data types to be passed to $stack argument
|
||||
$inputs = array(
|
||||
|
||||
// int data
|
||||
/*1*/ 'int' => array(
|
||||
0,
|
||||
1,
|
||||
12345,
|
||||
-2345,
|
||||
),
|
||||
|
||||
// float data
|
||||
/*2*/ 'float' => array(
|
||||
10.5,
|
||||
-10.5,
|
||||
12.3456789000e10,
|
||||
12.3456789000E-10,
|
||||
.5,
|
||||
),
|
||||
|
||||
// null data
|
||||
/*3*/ 'null' => array(
|
||||
NULL,
|
||||
null,
|
||||
),
|
||||
|
||||
// boolean data
|
||||
/*4*/ 'bool' => array(
|
||||
true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
),
|
||||
|
||||
// empty data
|
||||
/*5*/ 'empty string' => array(
|
||||
"",
|
||||
'',
|
||||
),
|
||||
|
||||
/*6*/ 'empty array' => array(
|
||||
),
|
||||
|
||||
// string data
|
||||
/*7*/ 'string' => array(
|
||||
"string",
|
||||
'string',
|
||||
$heredoc,
|
||||
),
|
||||
|
||||
// object data
|
||||
/*8*/ 'object' => array(
|
||||
new classA(),
|
||||
),
|
||||
|
||||
// undefined data
|
||||
/*9*/ 'undefined' => array(
|
||||
@$undefined_var,
|
||||
),
|
||||
|
||||
// unset data
|
||||
/*10*/ 'unset' => array(
|
||||
@$unset_var,
|
||||
),
|
||||
|
||||
// resource variable
|
||||
/*11*/ 'resource' => array(
|
||||
$fp
|
||||
),
|
||||
);
|
||||
|
||||
// loop through each element of $inputs to check the behavior of array_shift
|
||||
$iterator = 1;
|
||||
foreach($inputs as $key => $input) {
|
||||
echo "\n-- Iteration $iterator: $key data --\n";
|
||||
var_dump( array_shift($input) );
|
||||
var_dump($input);
|
||||
$iterator++;
|
||||
};
|
||||
|
||||
fclose($fp);
|
||||
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Iteration 1: int data --
|
||||
int(0)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(12345)
|
||||
[2]=>
|
||||
int(-2345)
|
||||
}
|
||||
|
||||
-- Iteration 2: float data --
|
||||
float(10.5)
|
||||
array(4) {
|
||||
[0]=>
|
||||
float(-10.5)
|
||||
[1]=>
|
||||
float(123456789000)
|
||||
[2]=>
|
||||
float(1.23456789E-9)
|
||||
[3]=>
|
||||
float(0.5)
|
||||
}
|
||||
|
||||
-- Iteration 3: null data --
|
||||
NULL
|
||||
array(1) {
|
||||
[0]=>
|
||||
NULL
|
||||
}
|
||||
|
||||
-- Iteration 4: bool data --
|
||||
bool(true)
|
||||
array(3) {
|
||||
[0]=>
|
||||
bool(false)
|
||||
[1]=>
|
||||
bool(true)
|
||||
[2]=>
|
||||
bool(false)
|
||||
}
|
||||
|
||||
-- Iteration 5: empty string data --
|
||||
string(0) ""
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(0) ""
|
||||
}
|
||||
|
||||
-- Iteration 6: empty array data --
|
||||
NULL
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 7: string data --
|
||||
string(6) "string"
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(6) "string"
|
||||
[1]=>
|
||||
string(11) "hello world"
|
||||
}
|
||||
|
||||
-- Iteration 8: object data --
|
||||
object(classA)#%d (0) {
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 9: undefined data --
|
||||
NULL
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 10: unset data --
|
||||
NULL
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 11: resource data --
|
||||
resource(%d) of type (stream)
|
||||
array(0) {
|
||||
}
|
||||
Done
|
||||
@@ -0,0 +1,188 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - Pass array with different data types as keys
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass arrays with different data types as keys to test how array_shift() re-assigns keys
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// heredoc string
|
||||
$heredoc = <<<EOT
|
||||
hello world
|
||||
EOT;
|
||||
|
||||
// unexpected values to be passed to $stack argument
|
||||
$inputs = array(
|
||||
|
||||
// int data
|
||||
/*1*/ 'int' => array(
|
||||
0 => 'zero',
|
||||
1 => 'one',
|
||||
12345 => 'positive',
|
||||
-2345 => 'negative',
|
||||
),
|
||||
|
||||
// float data
|
||||
/*2*/ 'float' => array(
|
||||
10.5 => 'positive',
|
||||
-10.5 => 'negative',
|
||||
.5 => 'half',
|
||||
),
|
||||
|
||||
/*3*/ 'extreme floats' => array(
|
||||
12.3456789000e10 => 'large',
|
||||
12.3456789000E-10 => 'small',
|
||||
),
|
||||
|
||||
// null data
|
||||
/*4*/ 'null uppercase' => array(
|
||||
NULL => 'null 1',
|
||||
),
|
||||
|
||||
/*5*/ 'null lowercase' => array(
|
||||
null => 'null 2',
|
||||
),
|
||||
|
||||
// boolean data
|
||||
/*6*/ 'bool lowercase' => array(
|
||||
true => 'lowert',
|
||||
false => 'lowerf',
|
||||
),
|
||||
|
||||
/*7*/ 'bool uppercase' => array(
|
||||
TRUE => 'uppert',
|
||||
FALSE => 'upperf',
|
||||
),
|
||||
|
||||
// empty data
|
||||
/*8*/ 'empty double quotes' => array(
|
||||
"" => 'emptyd',
|
||||
),
|
||||
|
||||
/*9*/ 'empty single quotes' => array(
|
||||
'' => 'emptys',
|
||||
),
|
||||
|
||||
// string data
|
||||
/*10*/ 'string' => array(
|
||||
"stringd" => 'stringd',
|
||||
'strings' => 'strings',
|
||||
$heredoc => 'stringh',
|
||||
),
|
||||
|
||||
// undefined data
|
||||
/*11*/ 'undefined' => array(
|
||||
@$undefined_var => 'undefined',
|
||||
),
|
||||
|
||||
// unset data
|
||||
/*12*/ 'unset' => array(
|
||||
@$unset_var => 'unset',
|
||||
),
|
||||
);
|
||||
|
||||
// loop through each element of $inputs to check the behavior of array_shift()
|
||||
$iterator = 1;
|
||||
foreach($inputs as $key => $input) {
|
||||
echo "\n-- Iteration $iterator : $key data --\n";
|
||||
var_dump( array_shift($input) );
|
||||
var_dump($input);
|
||||
$iterator++;
|
||||
};
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Iteration 1 : int data --
|
||||
string(4) "zero"
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(8) "positive"
|
||||
[2]=>
|
||||
string(8) "negative"
|
||||
}
|
||||
|
||||
-- Iteration 2 : float data --
|
||||
string(8) "positive"
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(8) "negative"
|
||||
[1]=>
|
||||
string(4) "half"
|
||||
}
|
||||
|
||||
-- Iteration 3 : extreme floats data --
|
||||
string(5) "large"
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(5) "small"
|
||||
}
|
||||
|
||||
-- Iteration 4 : null uppercase data --
|
||||
string(6) "null 1"
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 5 : null lowercase data --
|
||||
string(6) "null 2"
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 6 : bool lowercase data --
|
||||
string(6) "lowert"
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(6) "lowerf"
|
||||
}
|
||||
|
||||
-- Iteration 7 : bool uppercase data --
|
||||
string(6) "uppert"
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(6) "upperf"
|
||||
}
|
||||
|
||||
-- Iteration 8 : empty double quotes data --
|
||||
string(6) "emptyd"
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 9 : empty single quotes data --
|
||||
string(6) "emptys"
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 10 : string data --
|
||||
string(7) "stringd"
|
||||
array(2) {
|
||||
["strings"]=>
|
||||
string(7) "strings"
|
||||
["hello world"]=>
|
||||
string(7) "stringh"
|
||||
}
|
||||
|
||||
-- Iteration 11 : undefined data --
|
||||
string(9) "undefined"
|
||||
array(0) {
|
||||
}
|
||||
|
||||
-- Iteration 12 : unset data --
|
||||
string(5) "unset"
|
||||
array(0) {
|
||||
}
|
||||
Done
|
||||
@@ -0,0 +1,109 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - multi-dimensional arrays
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test popping elements from a sub-array and popping an array from an array
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
$stack_first = array(array(1, 2, 3), 'one', 'two');
|
||||
$stack_last = array ('zero', 'one', array (1, 2, 3));
|
||||
echo "\n-- Before shift: --\n";
|
||||
echo "---- \$stack_first:\n";
|
||||
var_dump($stack_first);
|
||||
echo "---- \$stack_last:\n";
|
||||
var_dump($stack_last);
|
||||
|
||||
echo "\n-- After shift: --\n";
|
||||
echo "---- Pop array from array:\n";
|
||||
echo "Returned value:\t";
|
||||
var_dump(array_shift($stack_first));
|
||||
echo "New array:\n";
|
||||
var_dump($stack_first);
|
||||
|
||||
echo "---- Pop element from array within array:\n";
|
||||
echo "Returned value:\t";
|
||||
var_dump(array_shift($stack_last[2]));
|
||||
echo "New array:\n";
|
||||
var_dump($stack_last);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Before shift: --
|
||||
---- $stack_first:
|
||||
array(3) {
|
||||
[0]=>
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
}
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[2]=>
|
||||
string(3) "two"
|
||||
}
|
||||
---- $stack_last:
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(4) "zero"
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[2]=>
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
}
|
||||
}
|
||||
|
||||
-- After shift: --
|
||||
---- Pop array from array:
|
||||
Returned value: array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
}
|
||||
New array:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(3) "two"
|
||||
}
|
||||
---- Pop element from array within array:
|
||||
Returned value: int(1)
|
||||
New array:
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(4) "zero"
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[2]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
int(2)
|
||||
[1]=>
|
||||
int(3)
|
||||
}
|
||||
}
|
||||
Done
|
||||
@@ -0,0 +1,45 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - call recursively
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Use the result of one call to array_shift
|
||||
* as the the $stack argument of another call to array_shift()
|
||||
* When done in one statement causes strict error messages.
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
$stack = array ( array ( array ('zero', 'one', 'two'), 'un', 'deux'), 'eins', 'zwei');
|
||||
|
||||
// not following strict standards
|
||||
echo "\n-- Incorrect Method: --\n";
|
||||
var_dump(array_shift(array_shift(array_shift($stack))));
|
||||
|
||||
$stack = array (array( array('zero', 'one', 'two'), 'un', 'deux'), 'eins', 'zwei');
|
||||
// correct way of doing above:
|
||||
echo "\n-- Correct Method: --\n";
|
||||
$result1 = array_shift($stack);
|
||||
$result2 = array_shift($result1);
|
||||
var_dump(array_shift($result2));
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Incorrect Method: --
|
||||
|
||||
Strict Standards: Only variables should be passed by reference in %s on line %d
|
||||
|
||||
Strict Standards: Only variables should be passed by reference in %s on line %d
|
||||
string(4) "zero"
|
||||
|
||||
-- Correct Method: --
|
||||
string(4) "zero"
|
||||
Done
|
||||
@@ -0,0 +1,83 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - Referenced arrays
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test how array_shift when passed:
|
||||
* 1. a variable that is referenced to an array
|
||||
* 2. an array that contains a referenced array
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
echo "\n-- Variable is referenced array --\n";
|
||||
$original_array = array('zero', 'one', 'two');
|
||||
$copied_array = &$original_array;
|
||||
|
||||
echo "Result: ";
|
||||
var_dump(array_shift($copied_array));
|
||||
echo "\n\$original_array:\n";
|
||||
var_dump($original_array);
|
||||
echo "\n\$copied_array:\n";
|
||||
var_dump($copied_array);
|
||||
|
||||
echo "\n-- Element is referenced array --\n";
|
||||
$new_array = array (&$copied_array, 1, 'two');
|
||||
echo "Result: ";
|
||||
var_dump(array_shift($new_array[0]));
|
||||
echo "\n\$new_array:\n";
|
||||
var_dump($new_array);
|
||||
echo "\n\$copied_array\n";
|
||||
var_dump($copied_array);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Variable is referenced array --
|
||||
Result: string(4) "zero"
|
||||
|
||||
$original_array:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(3) "two"
|
||||
}
|
||||
|
||||
$copied_array:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(3) "two"
|
||||
}
|
||||
|
||||
-- Element is referenced array --
|
||||
Result: string(3) "one"
|
||||
|
||||
$new_array:
|
||||
array(3) {
|
||||
[0]=>
|
||||
&array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
string(3) "two"
|
||||
}
|
||||
|
||||
$copied_array
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(3) "two"
|
||||
}
|
||||
Done
|
||||
@@ -0,0 +1,34 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - position of internal pointer
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test that the internal pointer is reset after calling array_shift()
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
$stack = array ('one' => 'un', 'two' => 'deux');
|
||||
|
||||
echo "\n-- Call array_shift() --\n";
|
||||
var_dump($result = array_shift($stack));
|
||||
|
||||
echo "\n-- Position of Internal Pointer in Passed Array: --\n";
|
||||
echo key($stack) . " => " . current ($stack) . "\n";
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Call array_shift() --
|
||||
string(2) "un"
|
||||
|
||||
-- Position of Internal Pointer in Passed Array: --
|
||||
two => deux
|
||||
Done
|
||||
@@ -0,0 +1,50 @@
|
||||
--TEST--
|
||||
Test array_shift() function : usage variations - maintaining referenced elements
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_shift(array &$stack)
|
||||
* Description: Pops an element off the beginning of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* From a comment left by Traps on 09-Jul-2007 on the array_shift documentation page:
|
||||
* For those that may be trying to use array_shift() with an array containing references
|
||||
* (e.g. working with linked node trees), beware that array_shift() may not work as you expect:
|
||||
* it will return a *copy* of the first element of the array,
|
||||
* and not the element itself, so your reference will be lost.
|
||||
* The solution is to reference the first element before removing it with array_shift():
|
||||
*/
|
||||
|
||||
echo "*** Testing array_shift() : usage variations ***\n";
|
||||
|
||||
// using only array_shift:
|
||||
echo "\n-- Reference result of array_shift: --\n";
|
||||
$a = 1;
|
||||
$array = array(&$a);
|
||||
$b =& array_shift($array);
|
||||
$b = 2;
|
||||
echo "a = $a, b = $b\n";
|
||||
|
||||
// solution: referencing the first element first:
|
||||
echo "\n-- Reference first element before array_shift: --\n";
|
||||
$a = 1;
|
||||
$array = array(&$a);
|
||||
$b =& $array[0];
|
||||
array_shift($array);
|
||||
$b = 2;
|
||||
echo "a = $a, b = $b\n";
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_shift() : usage variations ***
|
||||
|
||||
-- Reference result of array_shift: --
|
||||
|
||||
Strict Standards: Only variables should be assigned by reference in %s on line %d
|
||||
a = 1, b = 2
|
||||
|
||||
-- Reference first element before array_shift: --
|
||||
a = 2, b = 2
|
||||
Done
|
||||
Reference in New Issue
Block a user