Patrick
Patrick

Reputation: 821

Change value within array based on input in php

I am trying to locale the correct sub-array in order to change the count, if a specific value is present more than once.

I have the following code:

$trending = [];

foreach($hashtags as $hashtag) {
    if(in_array($hashtag->hashtag, $hashtags))
    {
       array_search()
    }
    else {
       array_push($trending, [
         'hashtag' => $hashtag->hashtag,
         'counts' => '1'
          ]);
    }
}

This gives me the following example outout:

array(3) { 
    [0]=> array(2) 
    { 
        ["hashtag"]=> "foobar" 
        ["counts"]=> "1" 
    } 
    [1]=> array(2) 
    { 
        ["hashtag"]=> "hashtags" 
        ["counts"]=> "1" 
    } 
    [2]=> array(2) 
    { 
        ["hashtag"]=> "imageattached" 
        ["counts"]=> "1" 
    } 
} 

So in the foreach loop and the if statement, i want to check for dublicates of hashtags, e.g. if the hashtag foobar exists more than one time, I don't want to create another dublicate in the array, but I want to change the count to 2

How do I find the correct "sub"-array, and change the count of this to 2, if a hashtag is present within $hashtags more than once??

The idea is, that I at the end can sort these arrays, and get the hashtag that is most common, by looking at the count.

Upvotes: 0

Views: 60

Answers (4)

haiderktk
haiderktk

Reputation: 125

It can be more linear of you can change your array structure but for the current this should work.

$trending = [];
$checker  = true;
foreach($hashtags as $hashtag) {
    foreach ($trending as $key =>$value) {
        if($value["hashtag"] == $hashtag->hashtag){
            $trending[$key]["counts"]++;            
            $checker = false;
        }
    }

if($checker) {
       array_push($trending, [
         'hashtag' => $hashtag->hashtag,
         'counts' => '1'
          ]);
    }
$checker  = true;    
}

Upvotes: 0

Spencer D
Spencer D

Reputation: 3486

Have you considered using a keyed array?

$trending = array();

foreach($hashtags as $hashtag) {
    if(!isset($trending[$hashtag])){
        $trending[$hashtag] = 1;
    }else{
        $trending[$hashtag] += 1;
    }
}

By using a keyed array, there is no duplication and you can easily check how frequently a hashtag is used by just accessing $trending[$hashtag]. Additionally, you can get the list of all hashtags in the trending array using $allHashtags = array_keys($trending);.

Of course, if your project specifications do not allow for this, then by all means, use a different approach, but that would be the approach I would take.

Upvotes: 0

Angrist
Angrist

Reputation: 157

The PHP method array_count_values might be of some help. http://php.net/manual/en/function.array-count-values.php

Upvotes: 0

tklg
tklg

Reputation: 2642

If you change the structure of your output, you could do something like this:

$trending = [];
foreach($hashtags as $tag) {
    if (isset($trending[$tag])) $trending[$tag]++;
    else $trending[$tag] = 1;
}

Which would result in $trending having the structure

array(2) {
    ["foobar"] => 1,
    ["hashtags"] => 2
}

Which could then be looped through with

foreach($trending as $tag => $count) {
    echo $tag . ' appears ' . $count . ' times.' . PHP_EOL;
}

Upvotes: 2

Related Questions