Hiding values in foreach.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CEJ
Forum Newbie
Posts: 8
Joined: Tue Mar 07, 2006 9:39 pm

Hiding values in foreach.

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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) {

}
Post Reply