Jump to content

[SOLVED] Looping Through Results - Produces Duplicates !!


Jexx

Recommended Posts

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]

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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.