简体   繁体   中英

PHP Variables for if statements

Ok, I am wanting to do something like this:

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

And than I'd like it to be able to do something like 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 I need it to build an array like so, if !empty($param_id)

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

But if $param_id is empty it should do the this instead:

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

I don't think I can concatenate it or can I?

Can someone please help me here? This is happening many many times throughout a function, so I don't want to use if... else... statements every single time. Would be too many, thinking of a 1 fast approach for all of them.

Thanks :)

EDIT, here is the function in question:

function ListGroups($checked = array(), $unallowed = array(), $order = array(), $param_id = 0)
{
    global $context, $smcFunc, $txt;

    // We'll need this for loading up the 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
        FROM {db_prefix}membergroups
        WHERE 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] as $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 to do a check for !empty($param_id), if so, it needs to build the $all_groups['groups'] array without the $param_id.

So will need to add in a check for if (!empty($params_id)) build the array like so: $all_groups['groups'][$params_id] else build it like this instead: $all_groups['groups'] . I don't want a bunch of if... else... statements in here, just a 1 or 5 liner would be 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 what $all_groups['groups'] looks like before this; if it was empty, I'd shorten this to:

$all_groups['groups'] = array(
    /* contents */
);

if (!empty($param_id)) {
    $all_groups['groups'] = array($param_id => $all_groups['groups']);
}
if (!empty($param_id)) {
    $which = &$all_groups['groups'][$param_id]
} else {
    $which = &$all_groups['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,
);

unset($which); // unset $which, if you want to use this variable
               // in this scope once again

Generally speaking, references are the solution. See @zerkms' answer.

However, if at all possible, I would try to redesign your data structures such that you don't have to resort to this type of conditional behavior. For example, using default as the default missing key:

$which = !empty($param_id) ? $param_id : 'default';
$all_groups['groups'][$which] = array( ... );

I don't know if it's possible in your case, but this could be easier to manage.

Other potential solutions:

$tmp = array( ... );
if ($cond) 
  $foo = $tmp;
else
  $bar = $tmp;

or:

function make_array( ... )
{
  return array( ... );
}

if ($cond)
  $foo = make_array( ... );
else
  $bar = make_array( ... );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM