0

I have an array with user group ids

$arr_user_group = array();

and a Query that select all users from my Database with their user group

array_push($arr_user_group, $row3['group']);

result with print_r($arr_user_group);

Array ( [0] => 2 [1] => 5 [2] => 3 [3] => 5 [4] => 1) 

now what i need is to assign each number ([...] => number(2,5,3,5,1)) a name like

1 = Admin 
2 = Member
3 = Ipsum
4 = Lorem
5 = Guest

...what is the best way to do this? my output on the page is for example "2" but i need the name "Member"

if( isset($arr_user_group[0]) && '5'=== $arr_user_group[1] ) {

 $arr_user_group[1] = "Group5"; 

} 

5 Answers 5

2
$arr_user_group = array(2, 5, 3, 5, 1);

$groupNames = array (1 => 'Admin', 
                     2 => 'Member',
                     3 => 'Ipsum',
                     4 => 'Lorem',
                     5 => 'Guest');

$lenght = count($arr_user_group);

for ($i = 0; $i < $lenght; $i++) {
    $arr_user_group[$i] = $groupNames[$arr_user_group[$i]];
}

echo '<pre>' . print_r($arr_user_group, true) . '</pre>';

//print

Array
(
    [0] => Member
    [1] => Guest
    [2] => Ipsum
    [3] => Guest
    [4] => Admin
)
Sign up to request clarification or add additional context in comments.

2 Comments

@Daan Timmer : yes, I just wrote the minimal answer without checking for errors. You could check with an isset for the group_name if necessary. Since the data come from a database anyway, if it was my project, I would create a table groups (id, name) and get directly the group name with the query and a join.
I would do the same indeed :-)
2
$arr_user_group = Array(2, 5, 3, 5, 1);

foreach($arr_user_group as &$group) {
    switch($group) {
        case 1: 
            $group = 'Admin';
            break;
        case 2: 
            $group = 'Member';
            break;
        case 3: 
            $group = 'Ipsum';
            break;
        case 4: 
            $group = 'Lorem';
            break;
        default:
            $group = 'Guest';
    }
}

print_r($arr_user_group);

Output:

Array
(
    [0] => Member
    [1] => Guest
    [2] => Ipsum
    [3] => Guest
    [4] => Admin
)

Note: As you are receiving the array from the database it would be better to create a new table (user_groups) with two columns: id and name. Then you can get the group name with a simple JOIN in your query.

1 Comment

the number after Group was only an example :) The usergroup range is from 0 to 5
1

For a more complex solution with switch this should work:

$arr_user_group_in = Array(2, 5, 3, 5, 1);
$arr_user_group = array_map(function ($id) { 

switch($id){

    case 1:
        return 'Admin';
        break;

    case 2:
        return 'Member';
        break;  

    case 3:
        return 'Ipsum';
        break;  

    case 4:
        return 'Lorem';
        break;  

    case 5:
        return 'Guest';
        break;

}

}, $arr_user_group_in);

print_r(array_combine($arr_user_group_in, $arr_user_group));
// output: Array ( [2] => Member [5] => Guest [3] => Ipsum [1] => Admin ) 

Note that the 5 as a duplicate key will only appear once in the result.

Comments

1

Or you can go this way if you want pre-defined names instead of groupX:

<?PHP

$arr_user_group = Array(2, 5, 3, 5, 1, 10, 0);
$arr_group_names = Array("None", "Admin", "Member", "Ipsum", "Lorem", "Guest");

foreach($arr_user_group as &$element) {
    $element = ($element < count($arr_group_names)) ? $arr_group_names[$element] : $arr_group_names[0];
}

print_r($arr_user_group);

output:

Array
(
    [0] => Member
    [1] => Guest
    [2] => Ipsum
    [3] => Guest
    [4] => Admin
    [5] => None
    [6] => None
)

You can of course set a different 'default' value. This way you can have your group names coming from a database, where your $arr_group_names is filled from a DB. Probably easier to manage instead of hard-coding them.

Update: I've used count() directly within the loop. This might occur a significant performance penalty if you have a lot of elements in $arr_user_group because each iteration count is recalculated. It is better to assign count() to a local temporary variable (outside the loop). But for this example it is easier to show it like this.

Comments

0

As simple as this?

foreach ($arr_user_group as &$id) {
    $id = "Group$id";
}

or

$arr_user_group = array_map(function ($id) { return "Group$id"; }, $arr_user_group);

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.