douchi0028 2013-08-14 08:00
浏览 41
已采纳

在php中比较数组的未识别的胶印警告

I'm getting some unindentified offset warnings in php.

<?php

/*****************************************************************/
/*****************************************************************/
/**** Queries Active Directory For Overview Off All Employees ****/
/*****************************************************************/
/*****************************************************************/

/*-------------*/
/* Credentials */
/*-------------*/

    $ldapserver = 'srv-dc-01.vex.local';
    $ldapuser = 'SamL@vex'; 
    $ldappass = 'xxxx!';
    $ldaptree = "OU=Users,OU=MyBusiness,DC=vex,DC=local";

    $secret = '}84lSB+-cdH{?[';

/*------------------------*/
/* Connect & Authenticate */
/*------------------------*/

    $ldapconn = ldap_connect($ldapserver) or die("Could not connect to LDAP server.");

    ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); // For LDAP v3

    $ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass) or die ("Error trying to bind: ".ldap_error($ldapconn));

/*------------------------*/
/* Query Active Directory */
/*------------------------*/

    $result = ldap_search($ldapconn,$ldaptree, "(cn=*)", array("cn","title","mail","telephonenumber","thumbnailphoto")) or die ("Error in search query: ".ldap_error($ldapconn));

    $data = ldap_get_entries($ldapconn, $result);

    $count = $data['count'];

?>

<html>
    <head>
        <title>sive</title>
        <style> 
            .box {
                float:left;
                width:400px;
                height:100px;
                border-style:dashed;
                border-width:2px;
                border-color:grey;
                padding:5px;
                margin:5px;
            }
            .picture {
                float:left;
                width:100px;
            }
        </style>
    </head>
    <body>

<?php

    for($i=0;$i<$count;$i++)
    {


        $blacklist = array("BES Admin","Network Administrator","SBSMonAcct","Directory Updater","NL Admin","Standard User","Standard User with administration links","SSL Admin");
        foreach ($blacklist as $key => $value){
            if($data[$i]["cn"][0] === $value) { continue; } 
        }

        /*if($data[$i]["cn"][0] === "BES Admin") { continue; }
        if($data[$i]["cn"][0] === "Network Administrator") { continue; }
        if($data[$i]["cn"][0] === "SBSMonAcct") { continue; }
        if($data[$i]["cn"][0] === "Directory Updater") { continue; }
        if($data[$i]["cn"][0] === "NL Admin") { continue; }
        if($data[$i]["cn"][0] === "Standard User") { continue; }
        if($data[$i]["cn"][0] === "Standard User with administration links") { continue; }
        if($data[$i]["cn"][0] === "SSL Admin") { continue; }
        if($data[$i]["cn"][0] === "Vente-Exclusive.com Club") { continue; }
        if($data[$i]["cn"][0] === "Vente-Exclusive.com Logistics") { continue; }
        if($data[$i]["cn"][0] === "SQLServer2005MSSQLUser\$WIN-EUGSO7LO7PY\$BLACKBERRY") { continue; }
        if($data[$i]["cn"][0] === "Sharepoint TFS") { continue; }
        if($data[$i]["cn"][0] === "Jobs Autoreply") { continue; }
        if($data[$i]["cn"][0] === "Printer Lookup2") { continue; }
        if($data[$i]["cn"][0] === "Printer Lookup") { continue; }
        if($data[$i]["cn"][0] === "VPN Lookup") { continue; }
        if($data[$i]["cn"][0] === "WallaceBackups") { continue; }
        if($data[$i]["cn"][0] === "Info - Yaminta") { continue; }
        if($data[$i]["cn"][0] === "Service - Yaminta") { continue; }
        if($data[$i]["cn"][0] === "QA -. Test Mailbox") { continue; }
        if($data[$i]["cn"][0] === "Vente-Exclusive.com Android App") { continue; }
        if($data[$i]["cn"][0] === "Vente-Exclusive.com Social") { continue; }
        if($data[$i]["cn"][0] === "FashionDays") { continue; }
        if($data[$i]["cn"][0] === "KTN - Vente-Exclusive Admin") { continue; }
        if($data[$i]["cn"][0] === "Skype") { continue; }
        if($data[$i]["cn"][0] === "WSSSERVICE") { continue; }
        if($data[$i]["cn"][0] === "Accounting Robot") { continue; }
        if($data[$i]["cn"][0] === "NL - Amsterdam - Main meeting room") { continue; }
        if($data[$i]["cn"][0] === "Ticketing Authenticator") { continue; }
        if($data[$i]["cn"][0] === "SQLServerMSSQLServerADHelperUser\$SRV-DC-01") { continue; }
        if($data[$i]["cn"][0] === "SQLServer2005SQLBrowserUser\$SRV-DC-01") { continue; }
        if($data[$i]["cn"][0] === "SQLServerMSSQLServerADHelperUser\$VEX-SRV-NL-01") { continue; }
        if($data[$i]["cn"][0] === "SQLServer2005SQLBrowserUser\$VEX-SRV-NL-01") { continue; }
        if($data[$i]["cn"][0] === "SQLServerMSSQLServerADHelperUser\$SRV-DC-02") { continue; }
        if($data[$i]["cn"][0] === "SQLServer2005SQLBrowserUser\$SRV-DC-02") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_London") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_NewYork") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_Paris") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_Milan") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_Antwerp") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_HongKong") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM65_02_Barcelona") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM111_01_Coton") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM111_01_Silk") { continue; }
        if($data[$i]["cn"][0] === "BE_HUM111_01_Leather") { continue; }
        if($data[$i]["cn"][0] === "0024d63772ec") { continue; }
        if($data[$i]["cn"][0] === "0c607621979d") { continue; }
        if($data[$i]["cn"][0] === "000085ffbde8") { continue; }
        if($data[$i]["cn"][0] === "0088653e354e") { continue; }
        if($data[$i]["cn"][0] === "Ops Monitor") { continue; }
        if($data[$i]["cn"][0] === "DG_IT_Logistics") { continue; }
        if($data[$i]["cn"][0] === "DG_StrongView") { continue; }
        if($data[$i]["cn"][0] === "OPS Support") { continue; }
        if($data[$i]["cn"][0] === "SYS Support") { continue; }*/


        echo "<div class=\"box\">";
        $userData = $data[$i];

        if(array_key_exists('thumbnailphoto', $userData))
        {
            $fileName = tempnam(sys_get_temp_dir(), 'vex');

            $imageParam = basename($fileName);
            $keyParam = md5($secret . $imageParam);

            $fp = fopen($fileName, "wb") or die("Can't open $fileName for writing");

            fwrite($fp, $userData['thumbnailphoto'][0]);
            fclose($fp);

            echo "<div class=\"picture\"><img src=\"pic.php?image=" . rawurlencode($imageParam) . "&key=" . rawurlencode($keyParam) . "\" width=\"96\"></div>";
        }

        echo "Full name: ". $data[$i]["cn"][0] ."<br />";

        if(isset($data[$i]["title"][0])) {
                echo "Title: ". $data[$i]["title"][0] ."<br />";
            } else {
                echo "Title: No title found<br />";
            }
        if(isset($data[$i]["mail"][0])) {
                echo "Email: <a href='mailto:". $data[$i]["mail"][0] ."'> ". $data[$i]["mail"][0] ."</a><br />";
            } else {
                echo "Email: No mail found<br />";
            }
        if(isset($data[$i]["telephonenumber"][0])) {
                echo "Phone: ". $data[$i]["telephonenumber"][0] ."<br /><br />";
            } else {
                echo "Phone: No number found<br /><br />";
            }
    echo "</div>";

    }

/*------------------*/
/* Close Connection */
/*------------------*/

ldap_unbind($ldapconn);

?>

    </body>
</html>

Thanks

edit:

error

Notice: Undefined offset: 8 in C:\wamp\www\vex\ad.php on line 72

What I want to do is: I've got an array, but in that array are values that I don't want in there (I can't do a more specific query (query with LDAP in an OU, but there are computers in there like 'Administrator' or 'Printer', wich I don't want in my overview because I only need "real" employees))

So there is my blacklist

It works if do if($data[$i]["cn"][0] === "blabla") { continue; }, but then i have to do that like 52 times, and a lot of continues...

edit:

    $skip = 0;

    foreach ($blacklist as $key => $value){
        if($data[$i]["cn"][0] === $value) { $skip = 1; } 

    }
    if($skip)
        continue;

This skips all the values that are in the blacklist array without 100000 notice warnings!

  • 写回答

1条回答 默认 最新

  • dsk49208 2013-08-14 08:04
    关注

    Your array only has the keys 0-7. If the value of $i is anything but one of those values, it is an undefined offset. print_r($blacklist); shows the keys and their values:

    Array
    (
        [0] => BES Admin
        [1] => Network Administrator
        [2] => SBSMonAcct
        [3] => Directory Updater
        [4] => NL Admin
        [5] => Standard User
        [6] => Standard User with administration links
        [7] => SSL Admin
    )
    

    Be sure that your variable $count is defined and has a value of nothing greater than 8, but I'm guessing that you want exactly 8:

    $count = count($blacklist);
    

    Or you can define your loop as:

    $blacklist = array("BES Admin","Network Administrator","SBSMonAcct","Directory Updater","NL Admin","Standard User","Standard User with administration links","SSL Admin");
    for($i=0; $i<count($blacklist); $i++){
        if($data[$i]["cn"][0] === $blacklist[$i]) { continue; } 
    }
    

    However, when working with arrays, I prefer to use foreach because it keeps track of the number of array items for you:

    $blacklist = array("BES Admin","Network Administrator","SBSMonAcct","Directory Updater","NL Admin","Standard User","Standard User with administration links","SSL Admin");
    foreach ($blacklist as $key => $value){
        if($data[$i]["cn"][0] === $value) { continue; } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?