Jexx Posted August 17, 2009 Share Posted August 17, 2009 Hi The following is part of a script that I use to retrieve details from a local LDAP database and present the results on screen: The Binding has been done prior to this. $dn = "cn=system"; $filter="(objectclass=*)"; $justthese = array('guid','cn'); $sr=ldap_list($ds, $dn, $filter, $justthese); $info = ldap_get_entries($ds, $sr); for ($n=0; $n<$info['count']; $n++) { $system_cn = $info[$n]['cn'][0]; $system_guid[$system_cn] = $info[$n]['guid'][0]; } foreach ($system_guid as $key => $value) { echo "<br><b>System Name : " . "<font color='yellow'>". $key ."</font></b>"; echo "<br><table><tr>"; $dn = "guid=$value"; $filter="(objectclass=user)"; $justthese = array('cn','part_id','followmember'); $sr=ldap_search($ds, $dn, $filter, $justthese); $info = ldap_get_entries($ds, $sr); for ($n=0; $n<$info['count']; $n++) { $part_id = $info[$n]['part_id'][0]; $usr = $info[$n]['cn'][0]; echo $part_id ." - ". $usr ."<br>"; } echo "<hr align='left' width= '20%'>"; echo "</table></tr>"; } The script works well and the results can be seem in the attached file 'output1.png' Each System has it's own 'group' of users : System Name : Local Kettle Unit 1 53 - Frank 134 - James 103 - Margaret System Name : Local Kettle Unit 3 53 - Frank 134 - James 43 - Mark However Melt Bin 4a has it's own users AND includes the users from System Local Kettle Unit 3 group System Name : Melt Bin 4a Local Kettle Unit 3 154 - Adam 87 - Paul 98 - Sarah 134 - James Looking at the attached output1.png, you can see '134 - James' appears twice. Once because he is a member of Local Kettle Unit 3 and Melt Bin 4a. The results from var_export($info) is shown in the attached var_export.txt. So how do I filter the results, so there are NO duplicate based on the 'part_id'. Can anyone help ?? Many Thanks [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/ Share on other sites More sharing options...
ignace Posted August 17, 2009 Share Posted August 17, 2009 So how do I filter the results, so there are NO duplicate based on the 'part_id'. Store the results instead of displaying them directly go over the result check if a duplicate exist, if one exists unset() it. Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/#findComment-900335 Share on other sites More sharing options...
Jexx Posted August 17, 2009 Author Share Posted August 17, 2009 Thanks for the reply. How do I do this ? I have tried saving the results into an array, then using array_unique, but I can seem to get this to work !! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/#findComment-900424 Share on other sites More sharing options...
Jexx Posted August 18, 2009 Author Share Posted August 18, 2009 Can anyone help me with this ?? Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/#findComment-901282 Share on other sites More sharing options...
ignace Posted August 18, 2009 Share Posted August 18, 2009 I think this should work: $array = array(..);//your stored result $sizeof = sizeof($array); for ($i = 0; $i < $sizeof; ++$i) { $v1 = $array[$i]; for ($j = $i + 1; $j <= $sizeof; ++$j) { $v2 = $array[$j]; if (!strcasecmp($v1, $v2)) { $array[$j] = null; } } } $array = array_values($array); Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/#findComment-901328 Share on other sites More sharing options...
roopurt18 Posted August 18, 2009 Share Posted August 18, 2009 As you print each group of users, do the following: 1) Before printing the group, create an empty list of users printed so far 2) For each user in the group a) If the user exists in the list of users printed so far, skip b) If the user does not exist in the list of users printed so far i) Print the user ii) Add to the list of printed users <?php $info = ldap_get_entries($ds, $sr); $UsersPrintedSoFar = array(); // EMPTY LIST OF PRINTED USERS FOR THE GROUP for ($n=0; $n<$info['count']; $n++) { $part_id = $info[$n]['part_id'][0]; $usr = $info[$n]['cn'][0]; // CHECK IF USER EXISTS IN $UsersPrintedSoFar $PrintedSoFarKey = $part_id . '.' . $usr; if( array_key_exists( $PrintedSoFarKey, $UsersPrintedSoFar ) ) { // USER EXISTS IN LIST, SO HAS ALREADY BEEN PRINTED continue; } // USER HAS NOT BEEN PRINTED YET echo $part_id ." - ". $usr ."<br>"; // Print User $UsersPrintedSoFar[$PrintedSoFarKey] = true; // Add to list } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/#findComment-901347 Share on other sites More sharing options...
Jexx Posted August 19, 2009 Author Share Posted August 19, 2009 Thanks to those who replied.. roopurt18 - this has worked, thanks for the explanation Quote Link to comment https://forums.phpfreaks.com/topic/170699-solved-looping-through-results-produces-duplicates/#findComment-901741 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.