array not going through php function in_array

Multi tool use
Multi tool use











up vote
1
down vote

favorite












I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.



  $words = file('stopWords.txt');

//var_dump($words);


When I try something like:



 if( in_array('able', $words) ) {
echo "match found";
}


Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.










share|improve this question






















  • can you provide an example of var_dump($words); output?
    – Ataur Rahman
    2 days ago








  • 3




    FILE_IGNORE_NEW_LINES - AKA Omit newline at the end of each array element. able != ablen Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    – ArtisticPhoenix
    2 days ago












  • the array is really big so var_dump outputs a lot, do you wants just a snippet?
    – Brian hernandez
    2 days ago










  • @ArtisticPhoenix how and why did this work?! Thank you so much
    – Brian hernandez
    2 days ago










  • It's magic. It's because the return has line endings, and in array is not fuzzy.
    – ArtisticPhoenix
    2 days ago















up vote
1
down vote

favorite












I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.



  $words = file('stopWords.txt');

//var_dump($words);


When I try something like:



 if( in_array('able', $words) ) {
echo "match found";
}


Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.










share|improve this question






















  • can you provide an example of var_dump($words); output?
    – Ataur Rahman
    2 days ago








  • 3




    FILE_IGNORE_NEW_LINES - AKA Omit newline at the end of each array element. able != ablen Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    – ArtisticPhoenix
    2 days ago












  • the array is really big so var_dump outputs a lot, do you wants just a snippet?
    – Brian hernandez
    2 days ago










  • @ArtisticPhoenix how and why did this work?! Thank you so much
    – Brian hernandez
    2 days ago










  • It's magic. It's because the return has line endings, and in array is not fuzzy.
    – ArtisticPhoenix
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.



  $words = file('stopWords.txt');

//var_dump($words);


When I try something like:



 if( in_array('able', $words) ) {
echo "match found";
}


Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.










share|improve this question













I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.



  $words = file('stopWords.txt');

//var_dump($words);


When I try something like:



 if( in_array('able', $words) ) {
echo "match found";
}


Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.







php arrays function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Brian hernandez

226




226












  • can you provide an example of var_dump($words); output?
    – Ataur Rahman
    2 days ago








  • 3




    FILE_IGNORE_NEW_LINES - AKA Omit newline at the end of each array element. able != ablen Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    – ArtisticPhoenix
    2 days ago












  • the array is really big so var_dump outputs a lot, do you wants just a snippet?
    – Brian hernandez
    2 days ago










  • @ArtisticPhoenix how and why did this work?! Thank you so much
    – Brian hernandez
    2 days ago










  • It's magic. It's because the return has line endings, and in array is not fuzzy.
    – ArtisticPhoenix
    2 days ago


















  • can you provide an example of var_dump($words); output?
    – Ataur Rahman
    2 days ago








  • 3




    FILE_IGNORE_NEW_LINES - AKA Omit newline at the end of each array element. able != ablen Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    – ArtisticPhoenix
    2 days ago












  • the array is really big so var_dump outputs a lot, do you wants just a snippet?
    – Brian hernandez
    2 days ago










  • @ArtisticPhoenix how and why did this work?! Thank you so much
    – Brian hernandez
    2 days ago










  • It's magic. It's because the return has line endings, and in array is not fuzzy.
    – ArtisticPhoenix
    2 days ago
















can you provide an example of var_dump($words); output?
– Ataur Rahman
2 days ago






can you provide an example of var_dump($words); output?
– Ataur Rahman
2 days ago






3




3




FILE_IGNORE_NEW_LINES - AKA Omit newline at the end of each array element. able != ablen Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago






FILE_IGNORE_NEW_LINES - AKA Omit newline at the end of each array element. able != ablen Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago














the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago




the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago












@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago




@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago












It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago




It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Instead of just



 $words = file('stopWords.txt');


Use



$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);



flags



The optional parameter flags can be one, or more, of the following constants:



FILE_USE_INCLUDE_PATH
Search for the file in the include_path.



FILE_IGNORE_NEW_LINES
Omit newline at the end of each array element



FILE_SKIP_EMPTY_LINES
Skip empty lines




This is the most relevant one Omit newline at the end of each array element.



Basically this is what you are doing:



if( in_array('able', ["ablen"]) ) {
echo "match found";
}


Which is false. Or in other words its not exactly equivalent to doing explode("n", $contents) as the line (array items) still contain the newline. Which because in_array is not a fuzzy search (basically its ==) it won't match them 'able' != 'ablen'.




Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.




http://php.net/manual/en/function.file.php



You could also trim the items in the array like this $words=array_map('trim', $words); but why bother when there is a flag for it.



Cheers






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184053%2farray-not-going-through-php-function-in-array%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Instead of just



     $words = file('stopWords.txt');


    Use



    $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);



    flags



    The optional parameter flags can be one, or more, of the following constants:



    FILE_USE_INCLUDE_PATH
    Search for the file in the include_path.



    FILE_IGNORE_NEW_LINES
    Omit newline at the end of each array element



    FILE_SKIP_EMPTY_LINES
    Skip empty lines




    This is the most relevant one Omit newline at the end of each array element.



    Basically this is what you are doing:



    if( in_array('able', ["ablen"]) ) {
    echo "match found";
    }


    Which is false. Or in other words its not exactly equivalent to doing explode("n", $contents) as the line (array items) still contain the newline. Which because in_array is not a fuzzy search (basically its ==) it won't match them 'able' != 'ablen'.




    Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.




    http://php.net/manual/en/function.file.php



    You could also trim the items in the array like this $words=array_map('trim', $words); but why bother when there is a flag for it.



    Cheers






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      Instead of just



       $words = file('stopWords.txt');


      Use



      $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);



      flags



      The optional parameter flags can be one, or more, of the following constants:



      FILE_USE_INCLUDE_PATH
      Search for the file in the include_path.



      FILE_IGNORE_NEW_LINES
      Omit newline at the end of each array element



      FILE_SKIP_EMPTY_LINES
      Skip empty lines




      This is the most relevant one Omit newline at the end of each array element.



      Basically this is what you are doing:



      if( in_array('able', ["ablen"]) ) {
      echo "match found";
      }


      Which is false. Or in other words its not exactly equivalent to doing explode("n", $contents) as the line (array items) still contain the newline. Which because in_array is not a fuzzy search (basically its ==) it won't match them 'able' != 'ablen'.




      Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.




      http://php.net/manual/en/function.file.php



      You could also trim the items in the array like this $words=array_map('trim', $words); but why bother when there is a flag for it.



      Cheers






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Instead of just



         $words = file('stopWords.txt');


        Use



        $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);



        flags



        The optional parameter flags can be one, or more, of the following constants:



        FILE_USE_INCLUDE_PATH
        Search for the file in the include_path.



        FILE_IGNORE_NEW_LINES
        Omit newline at the end of each array element



        FILE_SKIP_EMPTY_LINES
        Skip empty lines




        This is the most relevant one Omit newline at the end of each array element.



        Basically this is what you are doing:



        if( in_array('able', ["ablen"]) ) {
        echo "match found";
        }


        Which is false. Or in other words its not exactly equivalent to doing explode("n", $contents) as the line (array items) still contain the newline. Which because in_array is not a fuzzy search (basically its ==) it won't match them 'able' != 'ablen'.




        Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.




        http://php.net/manual/en/function.file.php



        You could also trim the items in the array like this $words=array_map('trim', $words); but why bother when there is a flag for it.



        Cheers






        share|improve this answer














        Instead of just



         $words = file('stopWords.txt');


        Use



        $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);



        flags



        The optional parameter flags can be one, or more, of the following constants:



        FILE_USE_INCLUDE_PATH
        Search for the file in the include_path.



        FILE_IGNORE_NEW_LINES
        Omit newline at the end of each array element



        FILE_SKIP_EMPTY_LINES
        Skip empty lines




        This is the most relevant one Omit newline at the end of each array element.



        Basically this is what you are doing:



        if( in_array('able', ["ablen"]) ) {
        echo "match found";
        }


        Which is false. Or in other words its not exactly equivalent to doing explode("n", $contents) as the line (array items) still contain the newline. Which because in_array is not a fuzzy search (basically its ==) it won't match them 'able' != 'ablen'.




        Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.




        http://php.net/manual/en/function.file.php



        You could also trim the items in the array like this $words=array_map('trim', $words); but why bother when there is a flag for it.



        Cheers







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        ArtisticPhoenix

        14.6k11223




        14.6k11223






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184053%2farray-not-going-through-php-function-in-array%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            AKU,XbHB1nYsZzuEXNH6haetaLk3,cTv3 N7Vsopha ZivM KRUgo,VJBhsdv YlOEKkfl5n cf pscLj,R dynyFZ5bngvzn
            RaRpd,T1x4zTF vC j,3vh1ymNUK bieYev0e R,8AdQUVMViWoQZ46i

            Popular posts from this blog

            横浜市

            Rostock

            Europa