简体   繁体   中英

in_array not produced expected results

Some simple logic using in_array but I cannot get the expected result. I am expecting both IF statements to be true. I only get the second (TRUE_TEST2) to work.

I am going a bit loopy on this! I have var_dump'd everything and can see they are both identical strings of 11 chars. Why will one work and not the other?

Thanks

PHP Code

        $follow_topics = get_user_meta(wp_get_current_user()->ID,'user_topic_follow');

        echo '1. '; var_dump($tracked_topic_slug);
        echo '2. '; var_dump('behavioural');
        echo '3. '; var_dump($follow_topics);

        echo '<br />';

        if( in_array($tracked_topic_slug, $follow_topics)) {echo 'TRUE_TEST1';}
        if( in_array('behavioural', $follow_topics)) {echo 'TRUE_TEST2';}

HTML OUTPUT

    1.string 'behavioural' (length=11)

    2.string 'behavioural' (length=11)

    3. array (size=2)
      0 => string 'clinical' (length=8)
      1 => string 'behavioural' (length=11)

    TRUE_TEST2

Looks really weird. Can you show us result of this:

var_dump('behavioural' === $tracked_topic_slug);
var_dump($tracked_topic_slug === $follow_topics[1]);

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