Req #47685 [Opn->Sus]: array_key_reorder
| From: | cmb@php.net | Date: | Sun, 30 Sep 2018 21:13:08 +0000 |
| Subject: | Req #47685 [Opn->Sus]: array_key_reorder | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-217332@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=47685&edit=1
ID: 47685
Updated by: cmb@php.net
Reported by: rweidner at techport80 dot com
Summary: array_key_reorder
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Arrays related
Operating System: Any
PHP Version: 5.2.9
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[2009-03-17 02:04:08] 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>";
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=47685&edit=1