简体   繁体   中英

php in_array with strings containing “&” or “space” in php

I have an array like :

$array = array('a & b' => 'A & B', 'c & d'=>'C & D');

$string = 'a & b';

when I do in_array(ucwords($string),$array) I get FALSE if I do array_key_exists($string, $array) I also get FALSE

any idea? thanks

Update: it seems that the way I collected $string is where the problem was ( using fuelphp Uri:: Segment(3) ) though visually the string collected seems identical for some reason it is not. Thanks for your quick input

Your $array is not actually an array?

$array = array('a & b' => 'A & B', 'c & d' => 'C & D');

Instead of:

$array = ('a & b' => 'A & B', 'c & d' => 'C & D');

It works as one would expect for me: http://codepad.org/U1WtWiGe

The problem with your code is that you've missed the array keyword infront of your array and thus it will not be an array.

$array = array('a & b' => 'A & B', 'c & d' => 'C & D');
         ^^^^^

On a side note I'm a bit surprised you've even managed it to run as you would get a parse error with the code you provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM