Boxerman Posted May 9, 2015 Share Posted May 9, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/ Share on other sites More sharing options...
Barand Posted May 9, 2015 Share Posted May 9, 2015 For those of us not sat on a network with active directory, the content of $groupArray would be an immense help to us. Can you post the output from var_export($groupArray); Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511248 Share on other sites More sharing options...
Boxerman Posted May 9, 2015 Author Share Posted May 9, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511256 Share on other sites More sharing options...
ginerjm Posted May 9, 2015 Share Posted May 9, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511257 Share on other sites More sharing options...
Boxerman Posted May 9, 2015 Author Share Posted May 9, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511260 Share on other sites More sharing options...
Barand Posted May 9, 2015 Share Posted May 9, 2015 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>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511262 Share on other sites More sharing options...
Boxerman Posted May 9, 2015 Author Share Posted May 9, 2015 Thanks for the reply! Just so i can understand what you are providing, does this script have to take static array values or can this be dynamic? As the array is different for every user. Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511263 Share on other sites More sharing options...
Solution Barand Posted May 9, 2015 Solution Share Posted May 9, 2015 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); Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511264 Share on other sites More sharing options...
Boxerman Posted May 9, 2015 Author Share Posted May 9, 2015 That makes complete sense, thanks for you time and effort dude. +1 from me. Quote Link to comment https://forums.phpfreaks.com/topic/296165-changing-array-output/#findComment-1511265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.