如何使用 PHP 和 PHP 迭代多维数组保存为 JSON/BSON?

我有这段代码,请随意跳到代码底部,我试图迭代数组,我留下了整个代码,以便它可以用于测试目的。


<?php


//Saves all the attributes of all entries into an array.

function cleanUpEntry( $entry ) {

  $retEntry = array();

  for ( $i = 0; $i < $entry["count"]; $i++ ) {

    if (is_array($entry[$i])) {

      $subtree = $entry[$i];

      //This condition should be superfluous so just take the recursive call

      //adapted to your situation in order to increase perf.

      if ( ! empty($subtree['dn']) and ! isset($retEntry[$subtree['dn']])) {

        $retEntry[$subtree['dn']] = cleanUpEntry($subtree);

      }

      else {

        $retEntry[] = cleanUpEntry($subtree);

      }

    }

    else {

      $attribute = $entry[$i];

      if ( $entry[$attribute]['count'] == 1 ) {

        $retEntry[$attribute] = $entry[$attribute][0];

      } else {

        for ( $j = 0; $j < $entry[$attribute]['count']; $j++ ) {

          $retEntry[$attribute][] = $entry[$attribute][$j];

        }

      }

    }

  }

  return $retEntry;

}


  $ldaprdn = "cn=read-only-admin,dc=example,dc=com";

  $ldappass ="password";

  $ldapuri = "ldap.forumsys.com";


  // Connecting to LDAP

  $ldapconn = ldap_connect($ldapuri)

          or die("That LDAP-URI was not parseable");


  //We need to set the LDAP Protocol Version or else it isn't able to bind properly to the LDAP server.

  ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);


  ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

  //We bind to the LDAP server using the previous credentials and location.

  $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);


  // Verify if bind was successful or not

  if ($ldapbind) {

      echo "LDAP bind successful...\n";

  } else {

      echo "LDAP bind failed...\n";

  }



牛魔王的故事
浏览 93回答 1
1回答

MMTTMM

假设只有两个级别,您可以轻松执行 2 次foreach 循环:<?php$arr&nbsp; &nbsp; =&nbsp; &nbsp;[&nbsp; &nbsp; 'dc=example,dc=com' => [&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'objectclass' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'top',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dcObject',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'organization'&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'o' => 'example.com',&nbsp; &nbsp; &nbsp; &nbsp; 'dc' => 'example'&nbsp; &nbsp; ],&nbsp; &nbsp; 'cn=admin,dc=example,dc=com' => [&nbsp; &nbsp; &nbsp; &nbsp; 'objectclass' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'simpleSecurityObject',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'organizationalRole'&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'cn' => 'admin',&nbsp; &nbsp; &nbsp; &nbsp; 'description' => 'LDAP administrator'&nbsp; &nbsp; ],&nbsp; &nbsp; 'uid=newton,dc=example,dc=com' => [&nbsp; &nbsp; &nbsp; &nbsp; 'sn' => 'Newton',&nbsp; &nbsp; &nbsp; &nbsp; 'objectclass' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'inetOrgPerson',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'organizationalPerson',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'person',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'top'&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'uid' => 'newton',&nbsp; &nbsp; &nbsp; &nbsp; 'mail' => 'newton@ldap.forumsys.com',&nbsp; &nbsp; &nbsp; &nbsp; 'cn' => 'Isaac Newton'&nbsp; &nbsp; ]];foreach($arr as $name => $settings) {&nbsp; &nbsp; $new[]&nbsp; =&nbsp; &nbsp;PHP_EOL."Name: {$name}";&nbsp; &nbsp; foreach($settings as $k => $v) {&nbsp; &nbsp; &nbsp; &nbsp; $new[]&nbsp; =&nbsp; &nbsp;"{$k}: ".((is_array($v))? implode(PHP_EOL."{$k}: ", $v) : $v);&nbsp; &nbsp; }}print_r(trim(implode(PHP_EOL, $new)));会给你:Name: dc=example,dc=comobjectclass: topobjectclass: dcObjectobjectclass: organizationo: example.comdc: exampleName: cn=admin,dc=example,dc=comobjectclass: simpleSecurityObjectobjectclass: organizationalRolecn: admindescription: LDAP administratorName: uid=newton,dc=example,dc=comsn: Newtonobjectclass: inetOrgPersonobjectclass: organizationalPersonobjectclass: personobjectclass: topuid: newtonmail: newton@ldap.forumsys.comcn: Isaac Newton至于json,您可能只需使用json_encode($arr)自动将其设为json(如评论中所述)。
打开App,查看更多内容
随时随地看视频慕课网APP