Jump to content

changing array output


Go to solution Solved by Barand,

Recommended Posts

Hey guys,

 

Hopefully just a quick one and easy for you smart guys, but i'm running into an issue.

 

The code im using:

<? $user = $adldap->user()->infoCollection($username, array('*'));
$groupArray = $user->memberOf; 
foreach ($groupArray as $group) {
   echo $group;
}?>

This displays something like this (as is):

CN=testnamehere,OU=Groups,OU=Employees,OU=UserAccounts,DC=contoso,DC=comCN=heresanother,OU=Distribution Lists,DC=contoso,DC=comCN=VPN,OU=_UserGroups,DC=contoso,DC=comCN=ManicMondayDL,OU=_UserGroups,DC=contoso,DC=com

 

As you can see its not formatting how i want, and everytime i time, i get errors.

 

My question is 2 part, how do i have itas new lines AND now do i get it to strip everything apart from the CN part (so only the first part excluding the CN so the wanted outcome is:

 

testnamehere

heresanother

VPN

ManicMonday

 

I hope that makes sense, i've been banging my head on the wall for the past few hours. Please help! 

 

Thanks guys!

 

Link to comment
https://forums.phpfreaks.com/topic/296165-changing-array-output/
Share on other sites

Sorry for the lack of information here:

 

array ( 0 => 'CN=testnamehere,OU=Groups,OU=Employees,OU=UserAccounts,DC=contoso,DC=com', 1 => 'CN=heresanother,OU=Distribution Lists,DC=contoso,DC=com', 2 => CN=VPN,OU=_UserGroups,DC=contoso,DC=com', 3 => 'CN=ManicMondayDL,OU=_UserGroups,DC=contoso,DC=com', )

 

Again, sorry for the lack of information here.

Each element of your array contains a comma separated string of 'key=value' items.

 

1 - explode each array element into an array using the comma as the separator.

2 - foreach element of this new 'exploded' array do another explode on the = sign and echo out the second element of this new array.

Thanks for the quick response here! 

 

I should silly i know but can you double check where i'm going wrong please?

 

$user = $adldap->user()->infoCollection($username, array('*'));
$groupArray = $user->memberOf; 
foreach ($groupArray as $group) {
   $group;
$string = explode(',',$group);
$string = explode('=',$group);
}

I'm honestly struggling to get this working with the above code, i've read the .php.net site a 100 times, my head just cant get a grip of this :( 

try

$groupArray =  array ( 
    0 => 'CN=testnamehere,OU=Groups,OU=Employees,OU=UserAccounts,DC=contoso,DC=com', 
    1 => 'CN=heresanother,OU=Distribution Lists,DC=contoso,DC=com', 
    2 => 'CN=VPN,OU=_UserGroups,DC=contoso,DC=com', 
    3 => 'CN=ManicMondayDL,OU=_UserGroups,DC=contoso,DC=com'
    );

foreach ($groupArray as $group) {
    $items = explode(',', $group);
    foreach($items as $element) {
        list($key, $value) = explode('=', $element);
        if ($key=='CN') {
            echo "$value<br>";
        }
    }
}
  • Solution

Make it a function and pass it the group array

function CNList($arr)
{
    $list='';
    foreach ($arr as $group) {
        $items = explode(',', $group);
        foreach($items as $element) {
            list($key, $value) = explode('=', $element);
            if ($key=='CN') {
                $list .= "$value<br>";
                break;
            }
        }
    }
    return $list;
}

echo CNList($groupArray);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.