From: cr.vegelin on
How do I update an array key without disturbing the element order ?
Suppose an existing array("FR", values ...)
where I want to change 0 => "FR" to "country" => "FR"
and keep the original element order.

TIA, Cor
From: Richard Quadling on
On 21 May 2010 10:56, <cr.vegelin(a)gmail.com> wrote:
> How do I update an array key without disturbing the element order ?
> Suppose an existing array("FR", values ...)
> where I want to change 0 => "FR" to "country" => "FR"
> and keep the original element order.
>
> TIA, Cor
>

Something like ...

function arrayKeyChange(array &$array, $oldKey, $newKey) {
if (!array_key_exists($oldKey, $array)) {
trigger_error('Requested key does not exist.', E_USER_WARNING);
return False;
}
if (array_key_exists($oldKey, $array) && array_key_exists($newKey, $array)) {
trigger_error('Will result in duplicate keys.', E_USER_WARNING);
return False;
}

// Split keys/values
$keys = array_keys($array);
$values = array_values($array);

// Find position of oldKey
$oldKeyPos = array_search($oldKey, $keys, True);

// Replace key
$keys[$oldKeyPos] = $newKey;

// Combine keys/values
$array = array_combine($keys, $values);

return True;
}


--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
From: cr.vegelin on

----- Original Message -----
From: "Richard Quadling" <rquadling(a)googlemail.com>
To: <cr.vegelin(a)gmail.com>
Cc: <php-general(a)lists.php.net>
Sent: Friday, May 21, 2010 1:30 PM
Subject: Re: [PHP] how to update array keys and keep element order ?


> On 21 May 2010 10:56, <cr.vegelin(a)gmail.com> wrote:
>> How do I update an array key without disturbing the element order ?
>> Suppose an existing array("FR", values ...)
>> where I want to change 0 => "FR" to "country" => "FR"
>> and keep the original element order.
>>
>> TIA, Cor
>>
>
> Something like ...
>
> function arrayKeyChange(array &$array, $oldKey, $newKey) {
> if (!array_key_exists($oldKey, $array)) {
> trigger_error('Requested key does not exist.', E_USER_WARNING);
> return False;
> }
> if (array_key_exists($oldKey, $array) && array_key_exists($newKey,
> $array)) {
> trigger_error('Will result in duplicate keys.', E_USER_WARNING);
> return False;
> }
>
> // Split keys/values
> $keys = array_keys($array);
> $values = array_values($array);
>
> // Find position of oldKey
> $oldKeyPos = array_search($oldKey, $keys, True);
>
> // Replace key
> $keys[$oldKeyPos] = $newKey;
>
> // Combine keys/values
> $array = array_combine($keys, $values);
>
> return True;
> }

Thanks Richard,

I will check your solution. I tried the following:

function ar_replacekeys ($array, $replacements)
{
foreach ($array as $key => $value) // 1)
{
foreach ($replacements as $oldkey => $newkey)
{
if ($key == $oldkey)
{
$array[$newkey] = $value; // 2)
unset($array[$key]); // 2)
}
}
}
}

Works fine, except that replaced elements are placed at the end. (2)
Also tested: foreach ($array as &$key => $value) (1)
but this gave "Key element cannot be a reference"



From: Al on


On 5/21/2010 5:56 AM, cr.vegelin(a)gmail.com wrote:
> How do I update an array key without disturbing the element order ?
> Suppose an existing array("FR", values ...)
> where I want to change 0 => "FR" to "country" => "FR"
> and keep the original element order.
>
> TIA, Cor
>

I short on time; but, it seems array_combine() or array_fill_keys() would do it
for you.

From: Richard Quadling on
On 21 May 2010 13:34, <cr.vegelin(a)gmail.com> wrote:
>
> ----- Original Message ----- From: "Richard Quadling"
> <rquadling(a)googlemail.com>
> To: <cr.vegelin(a)gmail.com>
> Cc: <php-general(a)lists.php.net>
> Sent: Friday, May 21, 2010 1:30 PM
> Subject: Re: [PHP] how to update array keys and keep element order ?
>
>
>> On 21 May 2010 10:56,  <cr.vegelin(a)gmail.com> wrote:
>>>
>>> How do I update an array key without disturbing the element order ?
>>> Suppose an existing array("FR", values ...)
>>> where I want to change 0 => "FR" to "country" => "FR"
>>> and keep the original element order.
>>>
>>> TIA, Cor
>>>
>>
>> Something like ...
>>
>> function arrayKeyChange(array &$array, $oldKey, $newKey) {
>> if (!array_key_exists($oldKey, $array)) {
>> trigger_error('Requested key does not exist.', E_USER_WARNING);
>> return False;
>> }
>> if (array_key_exists($oldKey, $array) && array_key_exists($newKey,
>> $array)) {
>> trigger_error('Will result in duplicate keys.', E_USER_WARNING);
>> return False;
>> }
>>
>> // Split keys/values
>> $keys = array_keys($array);
>> $values = array_values($array);
>>
>> // Find position of oldKey
>> $oldKeyPos = array_search($oldKey, $keys, True);
>>
>> // Replace key
>> $keys[$oldKeyPos] = $newKey;
>>
>> // Combine keys/values
>> $array = array_combine($keys, $values);
>>
>> return True;
>> }
>
> Thanks Richard,
>
> I will check your solution. I tried the following:
>
> function ar_replacekeys ($array, $replacements)
> {
>    foreach ($array as $key => $value)  // 1)
>    {
>        foreach ($replacements as $oldkey => $newkey)
>        {
>            if ($key == $oldkey)
>           {
>               $array[$newkey] = $value;    // 2)
>               unset($array[$key]);              // 2)
>           }
>        }
>    }
> }
>
> Works fine, except that replaced elements are placed at the end. (2)
> Also tested: foreach ($array as &$key => $value)    (1)
> but this gave "Key element cannot be a reference"
>
>
>
>

<?php
function arrayKeyChange(...){...} // As previously supplied.
$arr = array('Continent'=>'Europe', 'FR', 'Currency'=>'Euro');
arrayKeyChange($arr, 0, 'country');
arrayKeyChange($arr, 'country', 'Country');
print_r($arr);
?>

outputs ...

Array
(
[Continent] => Europe
[Country] => FR
[Currency] => Euro
)


--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling