亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHP 和 PHP 迭代多維數組保存為 JSON/BSON?

如何使用 PHP 和 PHP 迭代多維數組保存為 JSON/BSON?

PHP
牛魔王的故事 2023-12-15 15:41:05
我有這段代碼,請隨意跳到代碼底部,我試圖迭代數組,我留下了整個代碼,以便它可以用于測試目的。<?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";  }
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

假設只有兩個級別,您可以輕松執行 2 次foreach 循環:


<?php

$arr    =   [

    'dc=example,dc=com' => [

        

        'objectclass' => [

            'top',

            'dcObject',

            'organization'

        ],

        'o' => 'example.com',

        'dc' => 'example'

    ],

    'cn=admin,dc=example,dc=com' => [

        'objectclass' => [

            'simpleSecurityObject',

            'organizationalRole'

        ],

        'cn' => 'admin',

        'description' => 'LDAP administrator'

    ],

    'uid=newton,dc=example,dc=com' => [

        'sn' => 'Newton',

        'objectclass' => [

            'inetOrgPerson',

            'organizationalPerson',

            'person',

            'top'

        ],

        'uid' => 'newton',

        'mail' => '[email protected]',

        'cn' => 'Isaac Newton'

    ]

];


foreach($arr as $name => $settings) {

    $new[]  =   PHP_EOL."Name: {$name}";

    foreach($settings as $k => $v) {

        $new[]  =   "{$k}: ".((is_array($v))? implode(PHP_EOL."{$k}: ", $v) : $v);

    }

}


print_r(trim(implode(PHP_EOL, $new)));

會給你:


Name: dc=example,dc=com

objectclass: top

objectclass: dcObject

objectclass: organization

o: example.com

dc: example


Name: cn=admin,dc=example,dc=com

objectclass: simpleSecurityObject

objectclass: organizationalRole

cn: admin

description: LDAP administrator


Name: uid=newton,dc=example,dc=com

sn: Newton

objectclass: inetOrgPerson

objectclass: organizationalPerson

objectclass: person

objectclass: top

uid: newton

mail: [email protected]

cn: Isaac Newton

至于json,您可能只需使用json_encode($arr)自動將其設為json(如評論中所述)。


查看完整回答
反對 回復 2023-12-15
  • 1 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號