foreach echo issue with ldap array

Hey all,

used this site frequently to get me through hard times but this is my first time posting… hopefully someone can help!

I have a page which searches and filters my companies LDAP db and returns several lines of information of multiple people.

I am using foreach and echo to print the data but I can’t seem to figure out how to return the array key i need.

For example… I just want to be able to see the email address of everyone… but instead foreach is returning every line.

[php]
foreach( $info as $v1 ) {
foreach( $v1 as $v2 ) {
foreach ($v2 as $v3 ) {

echo “

”;			
echo $key ." " .$v3; }}}
echo “
”;
[/php]

This returns:

1 Abbey Road AC 1 John 1 K. 1 [email protected] 1 32412 1 Abbey Road 1 32412 1 Rob 1 [email protected] 1 32412 1 Abbey Road AC 1 Mate 1 Pete 1 [email protected] 1 32412 1 Abbey Road 1 Reeves 1 David 1 [email protected] 1 32412 1 Abbey Road 1 Cross-Tolley 1 Becks 1 [email protected] 1 32412 1 Abbey Road 1 Chris 1 Pile 1 [email protected] 1 32412 1 Abbey Road AC 1 Johnny 1 Borge 1 [email protected] 1 32412 1 Abbey Road 1 Fragoso 1 Pete 1 [email protected] 1 32412 1 Abbey Road 1 Beep 1 Mo 1 [email protected] 1 32412

This is the information: (1 Department 1 First Name 1 Last Name 1 Email 1 Division)

I want to pull just the email, I thought it would look something like this:

[php]
foreach( $info as $v1 ) {
foreach( $v1 as $v2 ) {
foreach ($v2 as $v3 ) {

echo “

”;			
echo $key ." " .$v3[mail]; }}}
echo “
”;
[/php]

or

[php]
foreach( $info as $v1 ) {
foreach( $v1 as $v2 ) {
foreach ($v2 as $v3[mail] ) {

echo “

”;			
echo $key ." " .$v3[mail]; }}}
echo “
”;
[/php]

but they don’t work

can anyone help?

Thanks

Hi there,

The best way I can help you is if you show us the results of the following, as it helps understand the structure:
[php]
echo “

”;
print_r($info);
echo “
”;
[/php]

If you do not want to post that information for security reasons, try recreating a smaller fake version with the same structure.

Hey!

here are 3 records from the code you asked me to enter:

[code]Array
(
[count] => 494
[0] => Array
(
[ou] => Array
(
[count] => 1
[0] => Abbey Road AC
)

        [0] => ou
        [sn] => Array
            (
                [count] => 1
                [0] => Phil
            )

        [1] => sn
        [givenname] => Array
            (
                [count] => 1
                [0] => Neil P.
            )

        [2] => givenname
        [mail] => Array
            (
                [count] => 1
                [0] => [email protected]
            )

        [3] => mail
        [departmentnumber] => Array
            (
                [count] => 1
                [0] => 6273
            )

        [4] => departmentnumber
        [count] => 5
        [dn] => abbeyID=55795,ou=people,o=abbey
    )

[1] => Array
    (
        [ou] => Array
            (
                [count] => 1
                [0] => Abbey Road
            )

        [0] => ou
        [sn] => Array
            (
                [count] => 1
                [0] => Intwood
            )

        [1] => sn
        [givenname] => Array
            (
                [count] => 1
                [0] => Robin
            )

        [2] => givenname
        [mail] => Array
            (
                [count] => 1
                [0] => [email protected]
            )

        [3] => mail
        [departmentnumber] => Array
            (
                [count] => 1
                [0] => 6273
            )

        [4] => departmentnumber
        [count] => 5
        [dn] => abbeyID=81302005,ou=people,o=abbey
    )

[2] => Array
    (
        [ou] => Array
            (
                [count] => 1
                [0] => Abbey Road AC
            )

        [0] => ou
        [sn] => Array
            (
                [count] => 1
                [0] => Matos
            )

        [1] => sn
        [givenname] => Array
            (
                [count] => 1
                [0] => Paul
            )

        [2] => givenname
        [mail] => Array
            (
                [count] => 1
                [0] => [email protected]
            )

        [3] => mail
        [departmentnumber] => Array
            (
                [count] => 1
                [0] => 6273
            )

        [4] => departmentnumber
        [count] => 5
        [dn] => abbeyID=81302017,ou=people,o=abbey
    )

[/code]

This is the exact same structure which is being printed but I have changed all the content for security purposes.

Many thanks for the help

so looking at the array with

 helped me understand the structure a little more.

I was able to accomplish the task just using a for loop and echo:
[php]
$arrayCount = $info[“count”];
for ( $counter = 0; $counter <= $arrayCount; $counter++) {
echo $info[$counter][mail][0];[/php]

prints out all the email address’s i need - this solves my issue is the most simple way… but I still cannot get it to work with the foreach loop.

thanks

Good stuff - glad you sorted it.

Just FYI, this worked for me when I recreated that array:
[php]
<?php
foreach($info as $person)
{
if(is_array($person))
{
foreach($person as $key1 => $details1)
{
if(is_array($details1) && $key1 == “mail”)
{
foreach($details1 as $val)
{
if(strpos($val,"@") !== false)
{
echo $val;
echo “
”;
}
}
}
}
}
else
{
null;
}
}
?>
[/php]

It will not work as a foreach look because of the structure unless you do something like the previous poster. It will also work as a while loop.

[php]

$arraycount = $info[‘count’];
$counter = 0;
while ($arraycount > $counter) {
echo $info[$counter][‘mail’][0]";
$counter ++;
}

[/php]

Also your code creates a blank line entry at the end of the statement because of the <= you’re starting at 0(the first entry) so you do not have to wait until $counter = $arrayCount because that is 1 more than the total in the array you simply have to wait until $arrayCount > $counter. I don’t know if the extra line is an issue for you but it was for me so I fixed it.

[php]

$arrayCount = $info[“count”];
for ( $counter = 0; $counter <= $arrayCount; $counter++) {
echo $info[$counter][mail][0];

[/php]

Thanks for posting your question it solved my problem.

Oops :o did not correct the code above! Here it is fixed.
[php]
$arrayCount = $info[“count”];
for ( $counter = 0; $counter < $arrayCount; $counter++) {
echo $info[$counter][mail][0];
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service