Jump to content

Match Words in Array & Retrieve Array Values


anevins

Recommended Posts

Hello,

 

I have an array $sentiment_example of strings and associated values.

I have another array $tweet_words of strings.

 

I want to check if a string in $sentiment_example matches a string in $tweet_words, then extract the associated value of that string from $sentiment_example.

 

Here's some code to help envisage it;


foreach ( $tweet_words as $tweet_word ) {

  if ( in_array ( $tweet_word, $sentiment_example ) ) {
    
    // But how do I extract the sentiment value for this word?

  }

}

 

Can anyone give me some tips, please?

$sentiment_example = array("Wrapped" => 6, "in" => 3, "my" => 5, "blanket" => 7, "always" => 4, "makes" => 6, "me" => 6,
						  "feel" => 5, "a" => 1, "little" => 2, "better" => 7, "when" => 3, "I'm" => 4, "not" => 3,
						  "feeling" => 5, "to" => 1, "well" => 6);		

 

$tweet_words is just the same as $sentiment_example but without the value, so just 17 strings.

I'm trying to simulate a sentiment analysis.

 

A sentence is broken up into individual words.

Those individual words ought to be compared against an array, which will contain strings of separate words from the entire English language, with a defined sentimental value assigned to each word.

 

It's one of the stages to find sentiment in a tweet, from Twitter.

So are you saying $tweet_words looks like this?

 

$tweet_words = array("Wrapped", "in", "my", "blanket", "always", "makes", "me",
  "feel", "a", "little", "better", "when", "I'm", "not",
  "feeling", "to", "well");

 

If so, what's the problem with posting it?

If not, what's the problem with posting it?

 

You do want help, don't you?

@Jesirose

 

Thanks for the help.

 

@Barand

 

I was hurried into responses, grateful for the quick responses, so I thought to save time rather than typing out that similar array.

I don't actually have the array $tweet_words, I have the string and I've put it into an array of separate strings per word, so I couldn't copy & paste it from my code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.