PHP:在Array中找不到键

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?

我的脚本在这里有问题。 我有这个数组: p> \ n

  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 
)
  code>  pre> 
 
 

我想在$ row(数组)时回显“Cake” TAETIGKEIT] == Berufschule使用此代码 p>

  if(strpos($ row ['TAETIGKEIT'],'Berufschule')=== true)echo“Cake”; 
   code>  pre> 
 
 

但回调永远不会被调用。 我也尝试直接比较 p>

  if($ row ['TAETIGKEIT  '] =='Berufschule')echo“Cake”; 
  code>  pre> 
 
 

但它也不起作用。 当我这样做时 p> print_r($ row ['TAETIGKEIT']; code> pre>

打印 p>

  Berufschule 
   code>  pre> 
 
 

我做错了什么? p> div>

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.

strpos never returns true - it returns false or integer value, so with === true you will never get pass.

Just try with:

strpos($row['TAETIGKEIT'], 'Berufschule') !== false