Forum Moderators: coopster

Message Too Old, No Replies

Custom function acting strangely

Looping through an array twice

         

MatthewHSE

9:15 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I spent some time this morning writing up a few quirky PHP functions just to get comfortable using them. I created one that's supposed to loop through an array, replace the underscores with spaces, and return the result as a new array. It works fine - except that it loops through the array twice and I don't know why. I know I must be telling it to somehow, but I sure don't know how or where. Here's the code:

// 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 . . .

jatar_k

9:22 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this for error checking, it isn't actually looping twice

<? 
// 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>";
}
?>

jatar_k

9:23 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do you know why?

think specifically about the keys

try echo each $key and then think about what the structure of the array coming in is. This is the core to passing and returning data

mcibor

9:30 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not looping twice.

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

MatthewHSE

10:23 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Much thanks to each of you, jatar_k for helping me think through the problem, and mcibor for pointing out the problem since my paltry PHP knowledge wasn't sufficient to see me through even with jatar_k's help!

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?

jatar_k

10:43 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



let's look at it line by line

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.