/    Sign up×
Community /Pin to ProfileBookmark

Increase array item value by 1

I have a word list based on some inputted text. I’m looping through the string which is exploded using PHP at each space.

I have an array and each word is placed into that array as a new item. Easy.

Now what I have is some words in my array multiple times. So I’m checking using the in_array function.

If it is I want to increase the value by one (It won’t always be 2 of course).

This is where I’m at.

“`
$titlewords = explode(” “, $row[‘Title’]);
foreach($titlewords as $singleword){

if (in_array($singleword, $words)) {
// Increase array item value by +1
} else {
array_push($words, $singleword); // Insert word as new array item with value of 1
}
}
“`

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 03.2020 — Sounds like maybe you could just use array_unique($titlewords) if you just want an array of unique words, or array_count_values($titlewords) if you also want to know how many occurrences there are.

https://php.net/array_unique

https://php.net/array_count_values
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...