php - 访问关联数组 php pdo 时出现问题

标签 php mysql arrays pdo

我在工作中遇到问题,我正在尝试使用使用提供的值

$row['make'] 

并通过导入将纯文本数据从我们的数据库中提取出来。现在,当我运行此代码时,我遇到了一个问题,我得到了这个

Notice: Undefined index: FORDX in convert.php on line 203

我不确定为什么会发生这种情况,我猜这与我访问数组的方式有关。

这是数组

$makes = array(
    "ACURA" => "Acura",//does not exist
    "AMRTR" => "American",
    "ATLAS" => "Atlas",//does not exist
    "BRRTR" => "Barrett",   
    "BMWXX" => "BMW",   //does not exist
    "BRAE" => "Brae",   //does not exist
    "BUDDX" => "Budd",
    "BUICK" => "Buick", //does not exist
    "BUTLR" => "Butler",    
    "CDLLC" => "Cadillac",  //does not exist
    "CNDCR" => "Can. Car",//does not exist
    "CPCTY" => "Capacity",
    "CPTRB" => "Capital",//does not exist
    "CARRR" => "Carrier",//does not exist
    "CHVRL" => "Chevrolet", 
    "CHRYS" => "Chrysler",  //does not exist
    "CLARK" => "Clark", //does not exist
    "CLLMN" => "Collins",   //does not exist
    "CMMRC" => "Commercial Truck", //does not exist     
    "CMMTR" => "Commercial Truck", //does not exist
    "CMMVN" => "Commercial Vans",   //does not exist
    "CRANE" => "Crane Carrier", //does not exist
    "CRNFR" => "Crane Fruehauf",    //does not exist
    "DATSN" => "Datsun",    //does not exist
    "DMNDB" => "Diamond B.",     //does not exist
    "DMNRE" => "Diamond Reo",   
    "DODGE" => "Dodge", 
    "DORSY" => "Dorsey",    
    "DRBTR" => "Durabody",   //does not exist
    "EASTX" => "East", 
    "ESPAR" => "Espar", //does not exist
    "FNTCN" => "Fontaine",  
    "FNTTR" => "Fontaine",  
    "FORDX" => "Ford",
    "FRDLT" => "Ford",      
    "FRGHT" => "Freightliner",  
    "FRECS" => "Frght Custom Chassis",  
    "FRUHF" => "Fruehauf",
    "FRYEX" => "Frye",  //does not exist
    "GMXXX" => "GM", //does not exist   
    "GMCCL" => "GMC",
    "GMCXX" => "GMC",   
    "GRAY" => "Grays",   //does not exist
    "GRTDN" => "Great Dane",    
    "GRMAL" => "Grumman",   //does not exist
    "HEILX" => "Heil",  
    "HIAB" => "Hiab",   //does not exist
    "HGHTR" => "Highway",   //does not exist
    "HINMT" => "Hino",  //does not exist
    "HBBMF" => "Hobbs", 
    "HOBBS" => "Hobbs", //does not exist
    "HOLND" => "Holland",   
    "HLLEQ" => "Holland Equipment", //does not exist    
    "HLLND" => "Holland Hitch Co",   //does not exist
    "HTCHN" => "Hutchens",  //does not exist
    "HTCIN" => "Hutchinson",    //does not exist
    "HYSTER" => "Hyster",   
    "INGRN" => "Ingersoll Rand",    
    "IHT" => "International",   
    "ISUZU" => "Isuzu", 
    "KNTCK" => "Kentucky",  
    "KNWRT" => "Kenworth", 
    "KINDRON" => "Kindron", //does not exist
    "KNGTB" => "King's", //does not exist
    "KRHNR" => "Krohnert",//does not exist
    "LAMDR" => "Land Rover", //does not exist
    "LFKTR" => "Lufkin",
    "MACK" => "Mack",
    "MCKTR" => "Mack",
    "MACKX" => "Mack",
    "MANAC" => "Manac",
    "MTLTR" => "Matlock", //does not exist
    "MXNIN" => "Maxon",//does not exist
    "MAZDA" => "Mazda",//does not exist
    "MRCBN" => "Mercedes-Benz",
    "MERC" => "Mercury",//does not exist
    "MCKBD" => "Mickey",//does not exist
    "MILLR" => "Miller",
    "MISCX" => "Miscellaneous",//does not exist
    "MITSUB" => "Mitsubishi",
    "FUSOX" => "Mitsubishi",
    "MTSBS" => "Mitsubishi",
    "MNDIN" => "Mond",//does not exist
    "MONON" => "Monon",
    "MORGN" => "Morgan",//does not exist
    "MNCPW" => "Munchie",//does not exist
    "M2" => "M2",//does not exist
    "NVSTR" => "Navistar",//does not exist
    "NSSDA" => "Nissan",//does not exist
    "NSSMT" => "Nissan",
    "OTHER" => "Other",
    "OTTST" => "Ottawa",
    "PALFGR" => "Palfinger", //does not exist
    "PTRBL" => "Peterbilt",
    "PTRSN" => "Peterson",//does not exist,
    "PINES" => "Pines",
    "POLAR" => "Polar",
    "PONT" => "Pontiac",//does not exist
    "PRVST" => "Prevost",//does not exist
    "PRINC" => "Princeton",//does not exist
    "RELIA" => "Reliable",//does not exist
    "RENLT" => "Renault",//does not exist
    "RSSIN" => "Roussy",//does not exist
    "SATRN" => "Saturn",//does not exist
    "STDMN" => "Steadman",//does not exist
    "STRLG" => "Sterling",
    "STWDR" => "Stewart",//does not exist
    "STGHT" => "Stoughton",//does not exist
    "STRCK" => "Strick",//does not exist
    "SUPRR" => "Superior",//does not exist
    "SPRRT" => "Superior Tech",//does not exist
    "SUPRM" => "Supreme",//does not exist
    "THRKN" => "Thermo King",//does not exist
    "TIMPT" => "Timpte",
    "TOYOT" => "Toyota",//does not exist
    "TRLMT" => "Trailermaster",//does not exist
    "TRLMB" => "Trailmobile",
    "TRLCN" => "Trailmobile",
    "TRNSI" => "Transicold",//does not exist
    "TRNST" => "Transit",//does not exist
    "TRIM" => "Trim",//does not exist
    "UTILIT" => "Utilit",//does not exist
    "UTLTY" => "Utility",
    "VLVNA" => "Volvo",//does not exist
    "VOLVO" => "Volvo",
    "WABSH" => "Wabash",
    "WALKR" => "Walker",
    "WARNI" => "Warner",//does not exist
    "WEBST" => "Webasto",//does not exist
    "WSTNK" => "Westank",//does not exist
    "WSTST" => "Western Star",
    "WSTAR" => "Westinghouse Air",//does not exist
    "WHITE" => "White"
);

当我循环遍历 $row['make'] 时来自数据库的示例数据

FORDX VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO GRTDN GRTDN 

这是循环代码

$result = $pdo->prepare("SELECT * FROM csvImport limit 20");


$data = array();
if ($result->execute()) {
    while($row = $result->fetch(PDO::FETCH_ASSOC))
    {
        if($row['status']== "P-Sale Pending")
            $status = "Pend";
        else
            $status = "Live";

        $unit = $row['unit'];//stocknumber
        $make = $makes[$row['make']];
    }
}

最佳答案

我知道这不是一个答案,而是一个调试技巧 - 但注释中的格式是不够的。

试试这个:

if (isset($makes[$row['make']]) {
    $make = $makes[$row['make']];
} else {
    echo "unrecognized make for unit {$unit}: ";
    var_dump($row['make']);
    die();
}

...我打赌其中一行的 make 列中有某种空白字符...

关于php - 访问关联数组 php pdo 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301750/

相关文章:

php - 在mysql表的列中按其位置(以逗号分隔)一次删除一个值

arrays - 为什么 bash 数组的一部分作为命令执行?

javascript - 从另一个实时页面共享 <div>

php - 如何通过codeigniter在mysql中插入多选下拉值( View -> Controller ->模型)

javascript - Jquery 插件 tinysort 不工作,不知道为什么

第二个表中最新状态 = 1 的 mySQL 记录

php - 从日期时间字段(mysql)中仅提取日期并将其分配给 php 变量

mysql - 困难的 MySQL 查询基于聚合的子数据确定父级的排名

Char** 字符串数组

c - 我在带有分隔符的文本文件中有一组值如何使用 C 编程修改值