Pioden Posted September 11, 2009 Share Posted September 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
Adam Posted September 11, 2009 Share Posted September 11, 2009 You can use a loop within a loop, you'd be best using foreach as well, much simpler. The manual contains example of how to loop through multi-dimensional arrays with foreach. Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 11, 2009 Share Posted September 11, 2009 Do echo "<pre>"; print_r($info); echo "</pre>"; It'll help the array make more sense Quote Link to comment Share on other sites More sharing options...
Adam Posted September 11, 2009 Share Posted September 11, 2009 Do echo "<pre>"; print_r($info); echo "</pre>"; It'll help the array make more sense ...Or just right click > view source. Quote Link to comment Share on other sites More sharing options...
Pioden Posted September 11, 2009 Author Share Posted September 11, 2009 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? Quote Link to comment Share on other sites More sharing options...
Adam Posted September 11, 2009 Share Posted September 11, 2009 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 />"; } } } Quote Link to comment Share on other sites More sharing options...
Pioden Posted September 11, 2009 Author Share Posted September 11, 2009 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! Quote Link to comment Share on other sites More sharing options...
Adam Posted September 11, 2009 Share Posted September 11, 2009 Try: foreach ($info as $i) { echo $i['uid'][0] . '<br />'; } Quote Link to comment Share on other sites More sharing options...
Pioden Posted September 11, 2009 Author Share Posted September 11, 2009 Genius! Much appreciated. Quote Link to comment 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.