anevins Posted September 12, 2012 Share Posted September 12, 2012 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 Is the matching value the KEY or the VALUE of the associative array? Post a sample of the arrays. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 12, 2012 Share Posted September 12, 2012 Sorry, can't envisage anything from that example. Quote Link to comment Share on other sites More sharing options...
anevins Posted September 12, 2012 Author Share Posted September 12, 2012 The value. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 Give us a sample of the arrays. You'll probably have to loop through the one array. You might be able to use array_intersect tho. Quote Link to comment Share on other sites More sharing options...
anevins Posted September 12, 2012 Author Share Posted September 12, 2012 $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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 Then you don't need $tweet_words at all. What are you trying to do? Quote Link to comment Share on other sites More sharing options...
anevins Posted September 12, 2012 Author Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 You already have the strings as the keys. if(isset($sentiments[$word])){ } Also, you should really move this data into a database. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 12, 2012 Share Posted September 12, 2012 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 BTW that "to" should be "too". Quote Link to comment Share on other sites More sharing options...
anevins Posted September 12, 2012 Author Share Posted September 12, 2012 @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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 12, 2012 Share Posted September 12, 2012 echo '<pre>' . print_r($tweet_words, 1) . '</pre>'; What does it output? Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted September 13, 2012 Share Posted September 13, 2012 Barand, give up man. You cannot help those who do not wish to be helped. Quote Link to comment Share on other sites More sharing options...
anevins Posted September 13, 2012 Author Share Posted September 13, 2012 I had already marked the thread resolved, why is there confusion? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.