anevins Posted September 13, 2012 Share Posted September 13, 2012 Hello, I have an array with multiple values, $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); I'm trying to multiply each value with an integer, foreach ( $sentiment_example as $word ) { $integer = 1; $multiplication += $integer * $anew_sentiment; } // Using $multiplication here Currently $multiplication holds 17 integers, I just want the last integer. How can I retrieve the last integer? Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/ Share on other sites More sharing options...
premiso Posted September 13, 2012 Share Posted September 13, 2012 Using substr: $lastInt = substr($multiplication, -1, 1); Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377586 Share on other sites More sharing options...
anevins Posted September 13, 2012 Author Share Posted September 13, 2012 Thanks I'll get on it once I get back Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377595 Share on other sites More sharing options...
hakimserwa Posted September 13, 2012 Share Posted September 13, 2012 Hello, I have an array with multiple values, $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); I'm trying to multiply each value with an integer, foreach ( $sentiment_example as $word ) { $integer = 1; $multiplication += $integer * $anew_sentiment; } // Using $multiplication here Currently $multiplication holds 17 integers, I just want the last integer. How can I retrieve the last integer? what do you want to arciheve? Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377603 Share on other sites More sharing options...
Jessica Posted September 13, 2012 Share Posted September 13, 2012 What if the integer is more than one digit? Again, you need to move this data into a database! Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377605 Share on other sites More sharing options...
anevins Posted September 13, 2012 Author Share Posted September 13, 2012 @hakimserwa I want to achieve $integer times the $word value, for each word. Then I want to add each result (of $integer * $word) into a variable, $multiplication. Using the $sentiment_example in my first post, $multiplication should hold only 74. @jesirose I don't see the consequence of an integer being more than one digit. The $sentiment_example is an extraction from a table within the database, it's already in there. Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377622 Share on other sites More sharing options...
Jessica Posted September 13, 2012 Share Posted September 13, 2012 Using substr: $lastInt = substr($multiplication, -1, 1); This won't work if it's more than one digit. Rather than doing it the way you are, if your data is already in a DB, there are easier ways to work with the data,. Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377628 Share on other sites More sharing options...
anevins Posted September 13, 2012 Author Share Posted September 13, 2012 I'm not sure how SQL will have access to the $sentiment_example variable and how I can separate sentences into individual words, is this what you mean ? Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377629 Share on other sites More sharing options...
Jessica Posted September 13, 2012 Share Posted September 13, 2012 Twice already in this and the other thread, you've been asked to explain the overall goal. If you do that, we can help. Otherwise you're going to get piecemeal advice which won't improve the overall program. Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377630 Share on other sites More sharing options...
anevins Posted September 13, 2012 Author Share Posted September 13, 2012 As I haven't explained properly, this is the sentiment-deriving process from tweets, Twitter that I want to implement; The average sentiment for the tweet text is calculated by finding the frequency that each word appears in the tweet, multiplying the sentiment for each word from the ANEW dataset by the frequency, then dividing theresulting value by the frequency Robert Hawkes (2011) I don't have the ANEW dataset yet, so I'm simulating it with $sentiment_example. Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377635 Share on other sites More sharing options...
Jessica Posted September 13, 2012 Share Posted September 13, 2012 You could definitely do all of that in SQL. Once you have the array of each of the words, you could use IN(). In your example you're multiplying by 1, so it's pointless. You also called this "addition", did multiplication, then asked for something unrelated. $multiplication += $integer * $anew_sentiment; Currently $multiplication holds 17 integers, I just want the last integer. How can I retrieve the last integer? It doesn't hold 17 integers, it holds the result of several computations. You're talking about using it as an array, and the help you got used it as a string, and you thought that would work. It's impossible to tell what you really want from the code you show us. Quote Link to comment https://forums.phpfreaks.com/topic/268326-addition/#findComment-1377641 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.