php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47685 array_key_reorder
Submitted: 2009-03-17 02:04 UTC Modified: 2018-09-30 21:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: rweidner at techport80 dot com Assigned:
Status: Suspended Package: Arrays related
PHP Version: 5.2.9 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rweidner at techport80 dot com
New email:
PHP Version: OS:

 

 [2009-03-17 02:04 UTC] rweidner at techport80 dot com
Description:
------------
Arbitrary ordering of an array

Reproduce code:
---------------
---
From manual page: function.array
---
function array_key_reorder($arrayToReorder, $keysOrder)
{
	if (!is_array($keysOrder))
	{
		$keysOrder = array($keysOrder);
	}
	
	$retval = array();
	
	for ($i = 0; $i < sizeof($keysOrder); $i++)
	{
		$key = $keysOrder[$i];
		if (array_key_exists($key, $arrayToReorder))
		{
			$retval[$key] = $arrayToReorder[$key];
		}
	}
	
	$keys = array_keys($arrayToReorder);
	for ($i = 0; $i < sizeof($arrayToReorder); $i++)
	{
		$key = $keys[$i];
		if (!in_array($key, $keysOrder))
		{
			$retval[$key] = $arrayToReorder[$key];
		}
	}
	
	return $retval;
}


Expected result:
----------------
This function will take arrayToReorder and arrange it in the order specified by keysOrder.

keysOrder can be a single key name which will be inserted as the first element of the new array.  If keysOrder is an array, the function will place all the keys in keyOrder (if they exist in the arrayToReorder) first in the array.  Keys that exist in the arrayToReorder but not in the keysOrder will be placed at the end of the array in thier relative original order.


Actual result:
--------------
// The following code works as expected

$array = array("red"=>"one", "green"=>"two", "blue"=>"three", "yellow"=>"four" );
echo "<p>The array looks like this...</p>";
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

echo "<p>Placing Yellow On Top.</p>";
$array = array_key_reorder($array, "yellow");
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

echo "<p>Placing Blue On Top.</p>";
$array = array_key_reorder($array, "blue");
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

echo "<p>Puttin' it back the way it was.</p>";
$array = array_key_reorder( $array, array("red", "green", "blue", "yellow") );
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

$array = array("red", "green", "blue", "yellow");
echo "<p>Working with numeric indexes</p>";
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

echo "<p>Placing Yellow On Top.</p>";
$array = array_key_reorder( $array, 3);
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

echo "<p>Placing Green On Top.</p>";
$array = array_key_reorder( $array, 1);
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

echo "<p>Puttin' it back the way it was.</p>";
$array = array_key_reorder( $array, array(0,1,2,3));
echo "<br /><pre>", print_r($array, 1), "</pre><br>";

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-16 19:02 UTC] levim@php.net
-Package: Feature/Change Request +Package: Arrays related -Operating System: any +Operating System: Any
 [2018-09-30 21:13 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-09-30 21:13 UTC] cmb@php.net
I have some doubts that this function is generally useful, and
since it can be implemented in userland, I think this request
requires an RFC.  Feel free to start the process[1].  For the time
being, I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Apr 05 01:01:24 2020 UTC