|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16119 Swapping two elements of an array
Submitted: 2002-03-16 18:52 UTC Modified: 2012-03-14 01:08 UTC
Avg. Score:4.0 ± 1.4
Reproduced:8 of 9 (88.9%)
Same Version:0 (0.0%)
Same OS:3 (37.5%)
From: joho at webbplatsen dot se Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.1.2 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-03-16 18:52 UTC] 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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-14 00:53 UTC] 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;
 [2012-03-14 01:08 UTC]
Fair point. Not really convinced we need this in the language anyway: it's easy 
enough to implement in userspace.
 [2012-03-14 01:08 UTC]
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Dec 12 16:01:26 2018 UTC