From:             rweidner at techport80 dot com
Operating system: any
PHP version:      5.2.9
PHP Bug Type:     Feature/Change Request
Bug description:  array_key_reorder

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 bug report at http://bugs.php.net/?id=47685&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=47685&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=47685&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=47685&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=47685&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=47685&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=47685&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=47685&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=47685&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=47685&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=47685&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=47685&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=47685&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=47685&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=47685&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=47685&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=47685&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=47685&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=47685&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=47685&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=47685&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=47685&r=mysqlcfg

Reply via email to