Page 1 of 1

Quick array question

Posted: Fri Oct 28, 2005 3:53 pm
by Ree
I have tested this:

Code: Select all

$details = array('First' => 'FirstValue', 'Second' => 'SecondValue', 'Third' => 'ThirdValue');

foreach ($details as $key => $value)
{
  switch ($key)
  {
    case 'First':
      $key = 'NewFirst';
      break;
    case 'Second':
      $key = 'NewSecond';
      break;
    case 'Third':
      $key = 'NewThird';
      break;
  }
}

foreach ($details as $key => $value)
{
  echo $key . ': ' . $value . '<br />';
}
It outputs:

First: FirstValue
Second: SecondValue
Third: ThirdValue

Although I thought it would print:

NewFirst: FirstValue
NewSecond: SecondValue
NewThird: ThirdValue

Obvioulsy, it doesn't work the way I thought. How do I change the key values then?

Posted: Fri Oct 28, 2005 4:42 pm
by RobertGonzalez
This is because you are reading the key of the Array as a variable. You are not resetting anything. Even though you are telling the script that as you walk the array, if it finds this particular key, change the variable known as key to this particular value, it doesn't change the key value of the array, just the key value in the foreach array loop.

I am looking for a way to do what you want. When I find I will post it.

Posted: Fri Oct 28, 2005 7:01 pm
by feyd
the only way to perform renaming of a key to an element is by destroying the element and adding the renamed key back in..

Posted: Fri Oct 28, 2005 7:12 pm
by RobertGonzalez
What about list = each'ing them into a new array?

Code: Select all

<?php
$details = array('First' => 'FirstValue', 'Second' => 'SecondValue', 'Third' => 'ThirdValue');

$newdetails = array();
while (list($key, $value) = each($details))
{
    $newkey = 'New' . $key;
    $newdetails[$newkey] = $value;
}

foreach ($newdetails as $key => $value)
{
  echo $key . ': ' . $value . '<br />';
} 
?>

Posted: Sat Oct 29, 2005 11:27 am
by John Cartwright
Why don't you try running your code? From what I can see it should work you expect.

Posted: Sat Oct 29, 2005 12:21 pm
by RobertGonzalez
Jcart wrote:Why don't you try running your code? From what I can see it should work you expect.
The variable $key is only available within the scope of the foreach loop. It is not changing the key but rather echoing the changed value of the key within the foreach loop. The key will still be the same after the foreach loop ends.

Posted: Sat Oct 29, 2005 12:42 pm
by Jenk

Code: Select all

<?php

$details = array('First' => 'FirstValue', 'Second' => 'SecondValue', 'Third' => 'ThirdValue');

foreach ($details as $key => $value)
{
      echo "New{$key}: {$value}<br />\n";
}

?>

Posted: Sat Oct 29, 2005 12:49 pm
by RobertGonzalez
Jenk wrote:

Code: Select all

<?php

$details = array('First' => 'FirstValue', 'Second' => 'SecondValue', 'Third' => 'ThirdValue');

foreach ($details as $key => $value)
{
      echo "New{$key}: {$value}<br />\n";
}

?>
I think the original poster wanted to actually change the value of $key, not just echo it out with a new name.
Ree wrote:Obvioulsy, it doesn't work the way I thought. How do I change the key values then?
I agree with Feyd. There is no way to destroy or change the value of key (without rewriting the array from top to bottom).

Posted: Sat Oct 29, 2005 1:02 pm
by Jenk

Code: Select all

<?php

$details = array('First' => 'FirstValue', 'Second' => 'SecondValue', 'Third' => 'ThirdValue');

foreach ($details as $key => $value)
{
    $details["New{$key}"] = $value;
    unset($details[$key]);
}

?>

Posted: Sat Oct 29, 2005 1:11 pm
by Ree
Everah wrote:I agree with Feyd. There is no way to destroy or change the value of key (without rewriting the array from top to bottom).
Yeah, that's what I did.

When posting the topic, I thought there might be some little trick I didn't know to change key value without declaring a new array which I see doesn't exist. Not a problem though ;).