Page 1 of 1

More elegant way to do this?

Posted: Mon Jul 26, 2010 9:49 am
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.

Re: More elegant way to do this?

Posted: Mon Jul 26, 2010 11:08 am
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']);

Re: More elegant way to do this?

Posted: Wed Jul 28, 2010 10:17 am
by tpainton
That works perfect. I knew there was a better way. Thanks.