arrays - PHP Variables for if statements -


ok, wanting this:

$which = !empty($param_id) ? "['groups'][$param_id]" : "['groups']"; 

and i'd able so...

$all_groups . $which = array(     -1 => array(     'id' => '-1',     'name' => $txt['parent_guests_only'],     'checked' => in_array('-1', $checked) || in_array('-3', $checked),     'is_post_group' => false, ) 

and need build array so, if !empty($param_id)

$all_groups['groups'][$param_id] = array(the array info); 

but if $param_id empty should instead:

$all_groups['groups'] = array(the array info); 

i don't think can concatenate or can i?

can please me here? happening many many times throughout function, don't want use if... else... statements every single time. many, thinking of 1 fast approach of them.

thanks :)

edit, here function in question:

function listgroups($checked = array(), $unallowed = array(), $order = array(), $param_id = 0) {     global $context, $smcfunc, $txt;      // we'll need loading names of each group.     if (!loadlanguage('manageboards'))         loadlanguage('manageboards');      if (empty($checked))         return array();      $all_groups['groups'][$param_id] = array();      if (!in_array('-1', $unallowed))         // guests         $all_groups['groups'][$param_id] = array(             -1 => array(                 'id' => '-1',                 'name' => $txt['parent_guests_only'],                 'checked' => in_array('-1', $checked) || in_array('-3', $checked),                 'is_post_group' => false,             )         );      if (!in_array('0', $unallowed))     {         // regular members         if (!empty($all_groups['groups']))             $all_groups['groups'][$param_id] += array(                 0 => array(                     'id' => '0',                     'name' => $txt['parent_members_only'],                     'checked' => in_array('0', $checked) || in_array('-3', $checked),                     'is_post_group' => false,                 )             );         else             $all_groups['groups'][$param_id] = array(                 0 => array(                     'id' => '0',                     'name' => $txt['parent_members_only'],                     'checked' => in_array('0', $checked) || in_array('-3', $checked),                     'is_post_group' => false,                 )             );     }      // load membergroups.     $request = $smcfunc['db_query']('', '         select group_name, id_group, min_posts         {db_prefix}membergroups         id_group > {int:is_zero}',         array(             'is_zero' => 0,         )     );     while ($row = $smcfunc['db_fetch_assoc']($request))     {         if (!in_array($row['id_group'], $unallowed))         {             $all_groups['groups'][(int) $param_id][(int) $row['id_group']] = array(                 'id' => $row['id_group'],                 'name' => trim($row['group_name']),                 'checked' => in_array($row['id_group'], $checked) || in_array('-3', $checked),                 'is_post_group' => $row['min_posts'] != -1,             );         }     }     $smcfunc['db_free_result']($request);      // let's sort these arrays accordingly!     if (!empty($order))     {         $all_groups['groups'][$param_id] = sortgroups($all_groups['groups'][$param_id], $order);         $context['group_order' . $param_id] = implode(', ', $order);     }     else     {         $context['group_order' . $param_id] = '';         sort($all_groups['groups'][$param_id]);         $x = 0;         foreach ($all_groups['groups'][$param_id] $key => $value)         {             $x++;             $context['group_order' . $param_id] .= $x < count($all_groups['groups'][$param_id]) ? $value['id'] . ', ' : $value['id'];         }     }      return $all_groups['groups'][$param_id]; } 

i need check !empty($param_id), if so, needs build $all_groups['groups'] array without $param_id.

so need add in check if (!empty($params_id)) build array so: $all_groups['groups'][$params_id] else build instead: $all_groups['groups']. don't want bunch of if... else... statements in here, 1 or 5 liner great!

thanks guys :)

don't overcomplicate it. :)

$array = array(     /* contents */ );  if (!empty($param_id)) {     $all_groups['groups'][$param_id] = $array; } else {     $all_groups['groups'] = $array; } 

i don't know $all_groups['groups'] looks before this; if empty, i'd shorten to:

$all_groups['groups'] = array(     /* contents */ );  if (!empty($param_id)) {     $all_groups['groups'] = array($param_id => $all_groups['groups']); } 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -