Quick array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Quick array question

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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 />';
} 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why don't you try running your code? From what I can see it should work you expect.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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";
}

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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]);
}

?>
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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 ;).
Post Reply