Bizarre Array Error


We are having problem with Arrays that is best described with code example:

the array is: $country_array

now when we check on the COUNT of items in this array or print them out, the CORRECT count & items are listed as per example:

echo '<p>country count: ’ . count($country_array);
for ($i = 0; $i < count($country_array); ++$i) {
echo ‘<br>’ . $country_array[$i];

printing our: 5
United States
United Kingdom

However when we do this:

if (isset($country_array) AND (in_array(‘United States’, $country_array))) {
echo ‘<p>United States’; }

if (isset($country_array) AND (in_array(‘United Kingdom’, $country_array))) {
echo ‘<p>United Kingdom’; }

if (isset($country_array) AND (in_array(‘Australia’, $country_array))) {
echo ‘Australia’; }

it ONLY prints out:
United States
and not the other matching items of this array!
WHAT the HEK is going on?


Could you copy and paste the real code please?

Do a var_dump($country_array);, chance are you have whitespace or non-displaying characters throwing off your latter code.

works fine for me… are you sure you’re not doing anything else between those lines of code?

^^ plus a [fphp]var_dump[/fphp] of $country_array please. I suspect there are leading or trailing spaces in there …


Sod it, cpradio beat me to it (:

I did the var_dump and saw this:

array(5) { [0]=> string(13) “United States” [1]=> string(14) “United Kingdom” [2]=> string(9) “Australia” [3]=> string(6) “Canada” [4]=> string(7) “Germany” }

So as you cans see the:
$country_array = explode(“,”, $country);

was leaving a blank space between all items after 1st item!

So i switched to:
$country_array = explode(", ", $country);

and now is working OK!

Sheeeeeesh :slight_smile: