douyue7408 2014-10-13 13:20
浏览 19
已采纳

PHP:在Array中找不到键

I have a problem with my script over here. I have got this array:

Array
(
    [KUNDENNUMMER] => 
    [BEZ] => 
    [DATUM] => 2014-10-10
    [VON] => 11:10:36
    [BIS] => 11:48:11
    [TAETIGKEIT] => Berufschule
    [BEZ_01] => 
    [DAUER] => 0001-01-01 00:37:00
    [STUNDEN] => 0.61
    [VERRECHENBAR] => F
    [BEMERKUNG] => 0x000c5cf2000000ba
    [USER_BEZ] => Armani, Kia
    [TZ_BEZ] => 
    [TT_VERRECHENBAR] => F
    [TT_ID] => 80
)

I want to echo "Cake" when $row (the array) [TAETIGKEIT] == Berufschule using this code

if(strpos($row['TAETIGKEIT'], 'Berufschule') === true) echo "Cake";

But the echo never gets called. I also tried to compare directly

if($row['TAETIGKEIT'] == 'Berufschule') echo "Cake";

but it did not work either. When I do

print_r($row['TAETIGKEIT'];

it prints

Berufschule

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • douyan8070 2014-10-13 13:28
    关注

    For summary, code in question didn't work for various reasons presumably.

    I can only assume, that the value of TAETIGKEIT has either a trailing or preceding space.

    if(strpos($row['TAETIGKEIT'], 'Berufschule') === true) echo "Cake";
    // doesn't work since strpos returns an integer if string is found or `false` if not.
    // it never returns true
    
    if($row['TAETIGKEIT'] == 'Berufschule') echo "Cake";
    // doesn't work due to the superfluous space, thus it's not exactly the same
    

    Solutions would be to use strpos correctly

    if(strpos($row['TAETIGKEIT'], 'Berufschule') !== false) echo "Cake";
    

    Or trim the values before comparison

    if(trim($row['TAETIGKEIT']) == 'Berufschule') echo "Cake";
    

    And as noted by h2ooooooo, var_dump would show these addtional spaces.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)