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; } 
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题