LMPX.COM |
Home | Linux | Mysql | PHP | XML | ||
|
|
|||
From: chris{at}w3style{dot}co{dot}uk@osu1.php.net Date: Sun Oct 5 19:06:30 2008 Subject: note 86148 added to function.array-shift
As pointed out earlier, in PHP4, array_shift() modifies the input array by-reference, but it doesn't return the first element by reference. This may seem like very unexpected behaviour. If you're working with a collection of references (in my case XML Nodes) this should do the trick.
<?php
/**
* This function exhibits the same behaviour is array_shift(), except
* it returns a reference to the first element of the array instead of a copy.
*
* @param array &$array
* @return mixed
*/
function &array_shift_reference(&$array)
{
if (count($array) > 0)
{
$key = key($array);
$first =& $array[$key];
}
else
{
$first = null;
}
array_shift($array);
return $first;
}
class ArrayShiftReferenceTest extends UnitTestCase
{
function testFunctionRemovesFirstElementOfNumericallyIndexedArray()
{
$input = array('foo', 'bar');
array_shift_reference($input);
$this->assertEqual(array('bar'), $input, '%s: The array should be shifted one element left');
}
function testFunctionRemovesFirstElementOfAssociativeArray()
{
$input = array('x' => 'foo', 'y' => 'bar');
array_shift_reference($input);
$this->assertEqual(array('y' => 'bar'), $input, '%s: The array should be shifted one element left');
}
function testFunctionReturnsReferenceToFirstElementOfNumericallyIndexedArray()
{
$foo = 'foo';
$input = array(&$foo, 'bar');
$first =& array_shift_reference($input);
$this->assertReference($foo, $first, '%s: The return value should reference the first array element');
}
function testFunctionReturnsReferenceToFirstElementOfAssociativeArray()
{
$foo = 'foo';
$input = array('x' => &$foo, 'y' => 'bar');
$first =& array_shift_reference($input);
$this->assertReference($foo, $first, '%s: The return value should reference the first array element');
}
function testFunctionReturnsNullIfEmptyArrayPassedAsInput()
{
$input = array();
$first = array_shift_reference($input);
$this->assertNull($first, '%s: Array has no first element so NULL should be returned');
}
}
?>
----
Server IP: 203.88.112.190
Probable Submitter: 203.206.182.17
----
Manual Page -- http://www.php.net/manual/en/function.array-shift.php
Edit -- https://master.php.net/note/edit/86148
Del: integrated -- https://master.php.net/note/delete/86148/integrated
Del: useless -- https://master.php.net/note/delete/86148/useless
Del: bad code -- https://master.php.net/note/delete/86148/bad+code
Del: spam -- https://master.php.net/note/delete/86148/spam
Del: non-english -- https://master.php.net/note/delete/86148/non-english
Del: in docs -- https://master.php.net/note/delete/86148/in+docs
Del: other reasons-- https://master.php.net/note/delete/86148
Reject -- https://master.php.net/note/reject/86148
Search -- https://master.php.net/manage/user-notes.php
| Navigate in group php.notes at sever news.php.net | |
| Previous | Next |
| © No Copyright You are free to use Anything |
Site Maintained by Zareef Ahmed
Powered By PHP Consultants |