solution

From: Date: Sun, 23 Jun 2002 22:38:44 +0000
Subject: solution
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-103501@lists.php.net to get a copy of this message
I figured it out, not what i'd wanted it to be, but it works, if anyone can improve upon it, please let me know. <?php function array_key_replace($foo,$original,$replacement,$replacement_value) {
    $bar=array();
    foreach($foo as $key=>$value) {
        if ($key==$original) {
            $bar[$replacement]=$replacement_value;
        }
        else $bar[$key]=$value;
    }
    return $bar;
} $foo=array("color"=>"blue","fruit"=>"apple","foo"=>"bar"); $foo=array_key_replace($foo,"fruit","car","pinto"); ?> Enjoy! ;) Austin W. Marshall wrote:
I can't seem to figure out how to use array_splice to replace an element while retaining the key... instead it replaces the key with 0 for example... $foo=array("color"=>"blue","fruit"=>"apple","foo"=>"bar"); array_splace($foo,1,1,array("car"=>"pinto")); yields... Array ( [color] => blue [0] => pinto [foo] => bar ) instead of Array ( [color] => blue [car] => pinto [foo] => bar ) ... Is there a quick and dirty way to do this? or does it require chopping the array up into to parts and merging them back together again.


« previous php.general (#103501) next »