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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号