0

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.

4 Answers 4

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh you beat me to that recommendation. +1 for the approach you recommended :P
0

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

1 Comment

Might be? What kind of answer is that
0

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.

Comments

0

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;    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.