Jump to content

[SOLVED] Array help please!


Pioden

Recommended Posts

Hi folks,

 

As a results of an ldap search I have an array (or maybe more than one) which I can't figure out how to process.

 

The output of print_r ($info); (where ifo is the array) gives me two results which look like this:

 

Array ( [count] => 2 [0] => Array ( [uid] => Array ( [count] => 1 [0] => user1 ) [0] => uid [count] => 1 [dn] => uid=user1,ou=Users,dc=mycollege,dc=ac,dc=uk ) [1] => Array ( [uid] => Array ( [count] => 1 [0] => user2 ) [0] => uid [count] => 1 [dn] => uid=user2,ou=Users,dc=mycollege,dc=ac,dc=uk ) ) 

 

Processing the arrays with array_values

$prawf = array_values($info);
while (list($key,$value) = each($info)) {
echo "$key : $value<br />";
}

 

gets me

 

count : 2

0 : Array

1 : Array

 

which to me suggests arrays within arrays. How on earth do I process this stuff to get meaningful values i.e. user1,user2 etc.

 

All assistance very much apperiated.

 

TIA

 

Huw

Link to comment
Share on other sites

Wow. Thank you both - I think I'm getting somewhere now.

 

Using Gareth's suggestion I now see:

Array
(
    [count] => 2
    [0] => Array
        (
            [uid] => Array
                (
                    [count] => 1
                    [0] => user1
                )

            [0] => uid
            [count] => 1
            [dn] => uid=user1,ou=Users,dc=mycollege,dc=ac,dc=uk
        )

    [1] => Array
        (
            [uid] => Array
                (
                    [count] => 1
                    [0] => user2
                )

            [0] => uid
            [count] => 1
            [dn] => uid=user2,ou=Users,dc=mycollege,dc=ac,dc=uk
        )

)

 

using foreach

(an example dragged kicking and screaming from the manual comments)

 

foreach ($info as $v1) {

    foreach ($v1 as $v2) {

        echo "$v2<br />";

    }

}

 

the output is:

Warning: Invalid argument supplied for foreach() in /var/www/huw/ldaptest.php on line 38

Array

uid

1

uid=user1,ou=Users,dc=mycollege,dc=ac,dc=uk

Array

uid

1

uid=user2,ou=Users,dc=mycollege,dc=ac,dc=uk

 

I'm a bit puzzled why it warns about invalid arguments and then prints a result though!!

 

Can I use foreach to just access the userx value in array uid?

Link to comment
Share on other sites

Your array spans into a third dimension, try:

 

foreach ($info as $v1) {
    foreach ($v1 as $v2) {
        if (is_array($v2)) {
            foreach ($v2 as $v3) {
                echo $v3;
            }
        } else {
            echo $v2 . "<br />";
        }
    }
}

Link to comment
Share on other sites

Just figured out the three dimensional thingy.

 

echo $info["0"]["uid"]["0"];

 

gives me exactly the data I need - but just for the first array. How do I loop it through the arrays to get the results?

 

foreach {

echo $info["0"]["uid"]["0"];

}

 

doesn't work. I guess I need some statement before {

 

BTW I really do appreciate your patience. I'm used to working with database results so this kind of array stuff is unknown territory!

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.