Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6958323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:07:28+00:00 2026-05-27T15:07:28+00:00

This works $arr = array_merge(array_diff($words, array(the,an)); Why doesn’t this work? $common consists of 40

  • 0

This works

$arr = array_merge(array_diff($words, array("the","an"));

Why doesn’t this work?

$common consists of 40 words in an array.

$arr = array_merge(array_diff($words, $common));

Is there another solution for this?

For Reference:

<?php
error_reporting(0);
$str1= "the engine has two ways to run: batch or conversational. In batch, expert system has all the necessary data to process from the beginning";

common_words($str1);

function common_words(&$string) { 

    $file = fopen("common.txt", "r") or exit("Unable to open file!");
    $common = array();

    while(!feof($file)) {
      array_push($common,fgets($file));
    }
    fclose($file);
    $words = explode(" ",$string);
    $arr = array_merge(array_diff($words, array("the","an")));
    print_r($arr);
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:07:29+00:00Added an answer on May 27, 2026 at 3:07 pm

    White-spaces are evil, sometimes..

    fgets with only one parameter will return one line of data from the filehandle provided.

    Though, it will not strip off the trailing new-line ("\n" or whatever EOL character(s) is used) in the line returned.

    Since common.txt seems to have one word per line, this is the reason why php won’t find any matching elements when you use array_diff.

    PHP: fgets – Manual

    parameter: length

    Reading ends when length – 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

    Rephrase:

    • All entries off $common will have a trailing line-break the way you are doing it now.

    Alternative solutions 1

    If you are not going to process the entries in common.txt I’d recommend you to take a look at php’s function file, and use that in conjunction with array_map to rtrim the lines for you.

    $common = array_map ('rtrim', file ('common.txt')); // will do what you want
    

    Alternative solutions 2

    After @MarkBaker saw the solution above he made a comment saying that you might as well pass a flag to file to make it work in the same manner, there is no need to call array_map to "fix" the entries returned.

    $common = file ('common.txt', FILE_IGNORE_NEW_LINES);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

E.g. why does val list:List[Any] = List[Int](1,2,3) work, but val arr:Array[Any] = Array[Int](1,2,3) fails
I was wondering if foreach() works when the array looks like this: arr_name[eggs] =
I'm trying to read an array from a cookie like this: var arr =
var a,b,c; var arr = [1,2,3]; [a,b,c] = arr; this code works perfectly in
I'd like to resize a ctypes array. As you can see, ctypes.resize doesn't work
Here is my array: arr[0]='A'; arr[1]='B'; .... I tried to post it this way:
I have this sample code where I'm defining an array but it doesn't compile:
This works (prints, for example, 3 arguments): to run argv do shell script echo
This works, but is it the proper way to do it??? I have a
This works just fine: protected void txtTest_Load(object sender, EventArgs e) { if (sender is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.