0

I want to format the output of PHP array into HTML, that will align properly. And how will remove the array and other stuffs? Also, I want to group it by "ou". Ou have 4 or more categories.

The output will be like this with html table

 sn      dn                                        mail
 user    cn=user,ou=web service,dc=domain,dc=com   [email protected]

Or like this

 sn      dn                                        mail                 ou
 user    cn=user,ou=web service,dc=domain,dc=com   [email protected]   web service

Please help me. Thank you

Here's the output without html

 array(1410) { ["count"]=> int(1409) [0]=> array(4) { ["sn"]=> array(2) {   ["count"]=> int(1) [0]=> string(11) "user" } [0]=> string(2) "sn" ["count"]=> int(1) ["dn"]=> string(56) "cn=user,ou=web service,dc=domain,dc=com" } [1]=>.....

Here's the PHP array

  $dn = 'dc=domain,dc=com';
  $filter = "(|(sn=$person*)(fullname=$person*))";
  $ldaparray = array ("ou", "sn", "fullname", "mail");
  $sr=ldap_search($ldapconn, $dn, $filter, $ldaparray);
  $info = ldap_get_entries($ldapconn, $sr);
  var_dump($info); 

Output using print_r

Array
(
[count] => 1409
[0] => Array
    (
        [sn] => Array
            (
                [count] => 1
                [0] => user
            )

        [0] => sn
        [count] => 1
        [dn] => cn=username,ou=webservice,dc=domain,dc=com
    )
[1] => Array
    (
        [sn] => Array
            (
                [count] => 1
                [0] => user
            )

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

        [1] => mail
        [count] => 2
        [dn] => uid=userid,ou=webser,dc=doamin,dc=com
    )
6
  • 1
    duplicate question, see this: stackoverflow.com/questions/27125276/… Commented Mar 10, 2015 at 6:29
  • I can't understand how to format my array like in that post Commented Mar 10, 2015 at 6:43
  • @Glavić pls help me with, I saw your post here stackoverflow.com/questions/27125276/… and I can't figure it out how it will works with my array Commented Mar 10, 2015 at 6:52
  • post your sample array. Commented Mar 10, 2015 at 7:21
  • @vartateInfo i already posted it Commented Mar 10, 2015 at 7:38

3 Answers 3

0

OMG, var_dump is not a function to write down a content of an array!!!

If you want to write down a content of array, you must use loop. The loop for arrays is called "foreach". So at the place, you want to view a content of array, try

<?
 foreach($info as $val) {
echo $val["count"]; // this write down a value of each "count" in array
/* All another array values are now available in variable $val with index you gave them*/
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

didnt work, when I use your script, the out is integer like this 11112222222222122222222222222222222222222222211222222222222222222222222222222222222222222222222222222222
Of course, that is values of $info, but only ["count"] indexes... you must to define in loop another indexes, to get, what you want... these numbers are values of $info["count"]. Try for exapmle echo $val["dn"]
0

Are you expecting just one result to be returned? If so, your result will be in $info[0]. So to display in a table, you could do this:

<table>
  <thead>
    <tr><td>Key</td><td>Value</td><tr>
  </thead>
  <tbody>
    <?php foreach($info[0] as $key => $value): ?>
    <tr>
      <td><?php echo $key; ?></td>
      <td><?php echo $value; ?></td>
    </tr>
    <?php endforeach; ?>
  </tbody>
</table>

That said, if your array has other nested arrays, you'll need to adjust your logic accordingly, or you may want to loop through and re-build the array based on the values you're interested in.

1 Comment

I have thousand of results to be returned
0
<?php
    echo '<pre>';
    print_r($info); //your array
    echo '</pre>';
?>

This works with nice indentations.

1 Comment

but i just need only the value to be displayed. Btw, thanks with this, my array looks better now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.