Page 1 of 1

Hiding values in foreach.

Posted: Sat Mar 11, 2006 12:37 pm
by CEJ
Is there any way to hide certain values from appearing in foreach. For example:

Code: Select all

foreach ($list as $value) {
     echo $value;
}
(Assuming $list is defined).

Let's say I wanted to hide a value called 'For my eyes only!'. How would I do that?

-CEJ

Posted: Sat Mar 11, 2006 12:40 pm
by John Cartwright

Code: Select all

foreach ($list as $value) {
   if ($value != 'For my Eyes only!') {
      echo $value;
   }
}
Sounds more like a bad design to me, why not unset elements of the array you don't to display, or more them to a different array.

Posted: Sat Mar 11, 2006 1:39 pm
by josh

Code: Select all

$hide = array('for_my_eyes_only', '123');

/*
do not rely on this, whoever you're hiding it from could insert:
$hide = array();
even if he did not have access to the file above.. he could also simply remove the array_diff below
*/

foreach(array_diff($hide, $list) as $key => $value) {

}