Question

I would like to add comma to each value output, example: A, B, C. Also I would like to ignore certain value by not output it.

Here is my code:

$result2 = ldap_search($ldapconn, "ou=group,dc=fake,dc=com", "memberuid=*$username*");
$entry1 = ldap_get_entries($ldapconn, $result2);
?>
</td>

<td>
<?php
$temp_array = array();
for ($i=0;$i<sizeof($entry1);$i++) {
    if (isset($entry1[$i]['cn'][0])) {
        if (strlen(trim($entry1[$i]['cn'][0]))!=0) {
            array_push($temp_array, $entry1[$i]['cn'][0]);
        }
    }

}
$usergroup = implode(',', $temp_array);
$usergroups = explode(",", $usergroup);

foreach($usergroups as $x=>$x_value) {
    switch ($x_value) {
        case "management":
        case "Team Leaders":
        case "superuser":

            $x_value = "";
            break;
    }
    echo $x_value;
}

So the expected result should be like this without showing the above 3 values in the switch case,

User A - IT, Marketing

Ignoring the the values above if User A has the values.

Était-ce utile?

La solution

As I mentioned in the comments, if you have an array, doing implode and then explode using the same delimiter achieves absolutely nothing. In your case $usergroups will be exactly the same as $temp_array.

You can greatly simplify your code like this:

// This is just an example using what I think your array contains
$temp_array = array('IT','management','superuser','Marketing');

// Write a list of words to ignore
$ignores = array("management", "Team Leaders", "superuser");

// Loop over your array and remove the entries that contain the ignored words
foreach ($temp_array as $key => $value) {
    if (in_array($value, $ignores)) {
        unset($temp_array[$key]);
    }
}

echo implode(',', $temp_array); // outputs: IT,Marketing
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top