Req #16119 [Opn->Wfx]: Swapping two elements of an array
| From: | aharvey@php.net | Date: | Wed, 14 Mar 2012 01:08:35 +0000 |
| Subject: | Req #16119 [Opn->Wfx]: Swapping two elements of an array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-168423@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
Updated by: aharvey@php.net
Reported by: joho at webbplatsen dot se
Summary: Swapping two elements of an array
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: All
PHP Version: 4.1.2
Block user comment: N
Private report: N
New Comment:
Fair point. Not really convinced we need this in the language anyway: it's easy
enough to implement in userspace.
Previous Comments:
------------------------------------------------------------------------
[2012-03-14 00:53:05] uramihsayibok at gmail dot com
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;
------------------------------------------------------------------------
[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