tags:

views:

1741

answers:

4

I have the following code:

while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
 {
  for ($i=0; $i<count($row); $i++)
  {
            (DO THING HERE)
   $row[$i] = str_replace("\n", " ", $row[$i]);
   $row[$i] = str_replace("\r", " ", $row[$i]);
  }

 }

I basically want to do, if the associative array key is equal to "email" (so $row['email']) then append "@gmail.com" to it.

A: 

I'm not sure if this is exactly what you want but:

$row['email'].='@gmail.com';

This will append @gmail.com to the email row.

ryeguy
+5  A: 

Use a foreach loop and get both the key and the value for the assoc array.

foreach($row as $key => &$value)
{
    if($key == 'email') $value .= "@gmail.com";
}

Also you should be using mysql_fetch_array($result, MYSQL_ASSOC) if you want an associative array returned.

A more efficient way to append to the email key would be something like:

if(isset($row['email']))
    $row['email'] .= '@gmail.com';

Instead of looping through all the columns.

Adam Peck
not only is this inefficient, its also wrong. He passed the MYSQL_NUM constant to it, so there are no associative keys. thanks
mysql_fetch_assoc() also fetches associative arrays
Ólafur Waage
Tom Haigh
Adam, you can't change $value inside the loop like that - as foreach operates on a copy of the real array. You would have to do $row[$key] .= '@gmail.com'. Cheers
eliego
adam peck, i see you edited your answer after realizing how wrong you were. is that a submission of defeat? I am ready for you to apoligize
Still wrong Adam.
Crescent Fresh
@theman When I originally answered the question I will admit I did not notice the MYSQL_NUM constant. I don't see why that is such a big issue to you.
Adam Peck
cause you downvoted my more-correct answer and then posted a rude comment
Yes i also see i forgot to get a reference. it has been corrected.
Adam Peck
@theman: why do you take issue with editing a question to correct it?
Tom Haigh
+5  A: 

See the MYSQL_NUM you have there? That is going to return your data using the column indexes as keys (ie 0, 1, 2, etc).

You must either

a) find out which column index the email field is and do:

while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
{
    // 'email' is in column number 5
    $row[5] .='@gmail.com';

    for ($i=0; $i<count($row); $i++)
    {
        $row[$i] = str_replace("\n", " ", $row[$i]);
        $row[$i] = str_replace("\r", " ", $row[$i]);
    }
}

b) OR you can change MYSQL_NUM to MYSQL_ASSOC, and do:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
    $row['email'] .='@gmail.com';

    foreach($row as &$value)
    {
        $value = str_replace("\n", " ", $value);
        $value = str_replace("\r", " ", $value);
    }
}

Note the "&" before $value to make it a reference.

I would do the latter (I prefer foreach to for :)

Crescent Fresh
way to completely bite my answer bro. i demand half your rep
@theman: this SO "heartbeat" implementation is buggy. I didn't even see your answer while I was writing mine.
Crescent Fresh
crescentfresh, i saw your first comment to mine, its all good, you wrote your response better, i was joking the whole time
+1  A: 

Paulo had it almost exactly right:

while ($row = mysql_fetch_assoc($result)) {
    foreach($row as $key => &$value) {
        if($key == 'email') $value .= '@gmail.com';
        $value = str_replace("\n", " ", $value);
        $value = str_replace("\r", " ", $value);
    }
}

Note the "&" in the foreach line. That means you're modifying the value in the $row array, not just a copy of it.

James Socol