Req #16119 [Com]: Swapping two elements of an array
| From: | uramihsayibok at gmail dot com | Date: | Wed, 14 Mar 2012 00:53:05 +0000 |
| Subject: | Req #16119 [Com]: Swapping two elements of an array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-168422@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=16119&edit=1
ID: 16119
Comment by: uramihsayibok at gmail dot com
Reported by: joho at webbplatsen dot se
Summary: Swapping two elements of an array
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: All
PHP Version: 4.1.2
Block user comment: N
Private report: N
New Comment:
You can do this in one statement with list().
list($ar[1], $ar[0]) = array($ar[0], $ar[1]);
Note that due to how list() works you CANNOT use a shorthand of
list($ar[1], $ar[0]) = $ar;
Previous Comments:
------------------------------------------------------------------------
[2002-03-16 18:52:05] joho at webbplatsen dot se
I must be getting old, but I cannot seem to find a function that swaps the data of two array
entries. Like so:
$ar[0] = "second";
$ar[1] = "first";
array_swap ($ar, 0, 1);
I have not overlooked the sort functions, that is not what I am after. But I may have overlooked
other possibilities. Searching for "swap" in the PHP .CHM (fairly recent) yielded no hits.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=16119&edit=1