Forum Moderators: coopster
// Function to loop through an array and remove underscores
function loop_array($array)
{
foreach ($array as $key => $value)
{
// Un-comment the next line and it will loop twice instead of just once
// $key = eregi_replace("_", " ", "$key");
$value = eregi_replace("_", " ", "$value");
$array["$key"] = "$value";
}
return $array;
}// Now test it:
$myarray = array(
'Key_1' => 'Value_1',
'Key_2' => 'Value_2',
'Key_3' => 'Value_3',
'Key_4' => 'Value_4'
);
$myarray = loop_array($myarray);
foreach ($myarray as $key => $value)
{
echo "<b>$key:</b> $value<br>";
}
Why would uncommenting the $key line in the foreach loop make the loop go twice? I don't understand it at all.
Any ideas?
Thanks,
Matthew
Of course this is entirely academic since I don't have the slightest actual use for this function, but still it's behavior that I don't understand, and thus a good learning opportunity . . .
<?
// Function to loop through an array and remove underscores
function loop_array($array) {
$counter = 0;
foreach ($array as $key => $value) {
// Un-comment the next line and it will loop twice instead of just once
$key = eregi_replace("_", " ", "$key");
$value = eregi_replace("_", " ", "$value");
$array[$key] = $value;
echo '<br>',$counter;
$counter++;
}
return $array;
}
// Now test it:
$myarray = array('Key_1' => 'Value_1','Key_2' => 'Value_2','Key_3' => 'Value_3','Key_4' => 'Value_4');
$myarray = loop_array($myarray);
echo '<p>';
foreach ($myarray as $key => $value) {
echo "<b>$key:</b> $value<br>";
}
?>
If you uncomment the line
$key = eregi_replace("_", " ", "$key");
then you get new key values.
So when you're declaring
$array["$key"] = "$value";
you are declaring a new values to array.
before it was "Key_1" => "Value_1"
and now you're adding value: "Key1" => "Value1"
It's completely new key!
Best regards!
Michal Cibor
I understand now what was happening, but I don't understand why. I thought the line in question would replace each key with a new value. Why is it that it instead adds a new key?
this is our array
$myarray = array('Key_1' => 'Value_1','Key_2' => 'Value_2','Key_3' => 'Value_3','Key_4' => 'Value_4');
1 foreach ($array as $key => $value) {
2 $key = eregi_replace("_", " ", "$key");
3 $value = eregi_replace("_", " ", "$value");
4 $array[$key] = $value;
} we pass the above array into the function and it enters our lines above
1. for every row in the array put the key name into $key and the value into $value. We are now treating each of those as strings inside this foreach.
2. replace all underscores from the value in $key with spaces and assign this new value to the variable $key. We have not changed the actual key in the array only the value of the variable $key.
3. replace all underscores from the value in $value with spaces and assign this new value to the variable $value
4. assign the value of the variable $value to the array at index of the variable $key. A bit confusing really. What it means is: if the key of $key exists already we give it a new value, if it does not exist we create a new row/set in our array.
so what could we change?
foreach ($array as $key => $value) {
unset($array[$key]);
$key = eregi_replace("_", " ", "$key");
$value = eregi_replace("_", " ", "$value");
$array[$key] = $value;
} now we take our key and value, assign them to variables and then unset the old key in the array. Now when we get to the key/value assignment it sets a new one every time.