More elegant way to do this?

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
tpainton
Forum Newbie
Posts: 2
Joined: Mon Jul 26, 2010 9:29 am

More elegant way to do this?

Post by tpainton »

There must me a more elegant way to do this.. It seems cumbersome and winded. I was wondering if anyone had a suggestion for creating the final keyed array.

Code: Select all

$occupants = array();
foreach ($targets as $target){
  if (target_is_concealed($target)){ //simply returns true or false.
    $hidden[] = $target;
    }
    else{
      $nothidden[] =$target;
    }
}
$occupants['hidden'] = $hidden;
$occupants['nothidden'] = $nothidden;
return $occupants;
Thanks for the tutelage.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: More elegant way to do this?

Post by AbraCadaver »

It may be even simpler depending on what target_is_concealed() does, but here is an untested example:

Code: Select all

$occupants['hidden'] = array_filter($targets, 'target_is_concealed');
$occupants['nothidden'] = array_diff($targets, $occupants['hidden']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tpainton
Forum Newbie
Posts: 2
Joined: Mon Jul 26, 2010 9:29 am

Re: More elegant way to do this?

Post by tpainton »

That works perfect. I knew there was a better way. Thanks.
Post Reply