On Jul 23, 2007, at 8:47 AM, Tim B wrote:

# Escape for output into html
foreach ($event as $v1)
{
   foreach ($v1 as $v2)
   {
      $v2 = htmlentities($v2, ENT_QUOTES, 'UTF-8');
      echo "{$v2}<br>";
   }
}


The problem is that by default $v1 and $v2 are not *references* to the elements in the array you are looping through. Instead, they are copies of the values, so changing them doesn't have any effect on the original array. There are a couple ways around this:

1) if you're using php 5, you can make the value variable in the foreach statement a reference by prefixing it with "&":

foreach ($array as &$value) {
   $value = htmlentities($value);
}

2) or you can use the array keys to reassign the value back to the original place in the array:

foreach ($array as $key => $value) {
   $array[$key] = htmlentities($value);
}

As Cliff said, you can find the full details here:
http://us.php.net/foreach
http://us.php.net/manual/en/language.references.php

-- Dell


_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php

Reply via email to