0

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 :)

5
  • Oh, my eyes! global, global!! ;-P Commented Dec 3, 2010 at 1:55
  • if (!loadLanguage('ManageBoards')) loadLanguage('ManageBoards'); — If it doesn't work the first time, it'll work the second time...? Commented Dec 3, 2010 at 1:57
  • Is there a reason you're naming the variable $all_groups['groups'], if you never refer to $all_groups just by itself? Commented Dec 3, 2010 at 2:04
  • lol @ !loadLanguage, dunno, I've been using this for awhile now, hehe. Seems to work all of the time. I really need to break down and understand this function one day. But for some reason, the first time, it seems to only determine if the language is loaded, than the 2nd time, if called, loads the actual language. Commented Dec 3, 2010 at 2:41
  • calling it $all_groups['groups'] just for the sake of clarity to me. Suppose it can also be just $all_groups by itself, but what does that matter anyways? The problem still exists. Commented Dec 3, 2010 at 2:43

3 Answers 3

3

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']);
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is not really all that helpful, because $all_groups gets returned at the end of the function, and I want to return only 1 array, both at the same time, depending on a parameter in the function, which is $param_id. If $param_id is empty than it should return $all_groups['groups']. Hold on, I'll post up the entire function for you guys here.
I edited my original question now with the entire function. 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']
@SoLo This function is a bit too lengthy and involved to recommend something specific. In your current code you're never making this if...else decision at all, so it's hard to tell you where and how to apply it. You're also simply returning $all_groups['groups'][$param_id] in the end, which makes it weird that you're building this complicated array to begin with... It does seem to me like this function could be refactored into something much simpler, if only the logic was a bit clearer to me.
The groups get listed within <input> elements of type="check", than I use $_POST to get the values to know which checkboxes should be checked for this. This is a permissions feature I am working on. Permissions of a Forum software! Groups are which groups are allowed to do something and which one's are not.
I'd like to reuse the function if at all possible so that way I don't have to rewrite it in a different way just to change the array keys of the variable array.
|
0
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

13 Comments

huh? What does the $which = array(...); do? Also, is that a pointer being used? Are their compatibility problems with using that approach?
Also, do I need to set $which = array(); first before doing this?
@SoLoGHoST: i've changed sample a little and added the working code
@SoLoGHoST: no. try my second sample code with !true and true
your non-brace style makes me want to scrub my eyes and edit your post
|
0

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( ... );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.