1

I have a table called tags which contains the tag_name and the question_id. Just like Stack Overflow, I can insert each tags by exploding them with a comma. That works.

But I also wanted to show all the tags. Let's say this is the result I get after selecting it from the tags table.

Array (
    [0] => Array (
        [tag_name] => tag-1,
        [question_id] => 1
    )
    [1] => Array (
        [tag_name] => tag-1,
        [question_id] => 2
    )
    [2] => Array (
        [tag_name] => tag-2,
        [question_id] => 3
    )

)

When I iterate through them, I get

tag-1
tag-1
tag-2

But that's not how I wanted it. I was thinking of something like

tag-1 × 2
tag-2

How can I achieve that? I'm using CodeIgniter but it doesn't really matter, because I only want to understand the logic of it.

Thanks in advance.

1
  • 3
    It seems you're trying to count the duplicate entries. So... COUNT() them and do a GROUP BY ;-) maybe even using HAVING in conjunction with COUNT(). That would work. Commented Aug 12, 2016 at 17:43

2 Answers 2

1

I haven't tested this but something like this should work:

<?php
$tag_array = [
    0 => [
        'tag_name' => 'tag-1',
        'question_id' => 1
    ],
    1 => [
        'tag_name' => 'tag-1',
        'question_id' => 2
    ],
    2 => [
        'tag_name' => 'tag-2',
        'question_id' => 3
    ],
];

$count_tags = array();

foreach($tag_array as $v)
{
    if(!isset($count_tags[$v['tag_name']]))
    {
        $count_tags[$v['tag_name']] = 0;
    }

    ++$count_tags[$v['tag_name']];
}

// Sort your tags from hi to low
arsort($count_tags);

foreach($count_tags as $k=>$v)
{
    echo $k.($v > 1 ? ' x '.$v : '').'<br>';
}

Outputs:

tag-1 × 2
tag-2

Or you can just do this in SQL:

select
    tag_name,
    count(tag_name) as tag_count
from
    tags
group by
    tag_name
order by
    count(tag_name) desc
Sign up to request clarification or add additional context in comments.

5 Comments

I saw the edit lol seems you saw my comments up there ;-) you didn't have that SQL part there.
@Fred-ii- haha, yep! I didn't notice the mysqli tag on the question at first so I didn't want to assume OP had access to the data source :-)
Well, they have a choice ;-)
I'll give it a try thanks everyone. but just one thing. Does it avoid the other duplicates?
@mzcoxfde you're welcome. Please see my edit because I added sorting if that's important to you
1

My answer to the problem:

$array = [
    0 => [
        'tag_name' => 'tag-1',
        'question_id' => 1
    ],
    1 => [
        'tag_name' => 'tag-1',
        'question_id' => 2
    ],
    2 => [
        'tag_name' => 'tag-2',
        'question_id' => 3
    ],
];

$count = [];
foreach ($array as $arr)
{
    if (empty($count[$arr['tag_name']]))
    {
        $count[$arr['tag_name']] = 1;
    }
    else
    {
        $count[$arr['tag_name']]++;

    }
}

foreach ($count as $key => $c)
{
    echo $key . (($c > 1) ? ' x' . $c : '') . '<br>';'
}

// outputs:
tag-1 x2
tag-2

2 Comments

This also might work but it looks that same as MonkeyZeuz's answer
Guess I forgot to refresh the page before answering, I'll close my answer if you like. (And we've got similar thinking, I suppose)

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.