1

I'm trying to show tags separated with comma fetched from certain section of my website. The PHP functions I've created are:

$fid = intval($fid);
$query = $db->query("
    SELECT p.message
    FROM ".TABLE_PREFIX."posts p
    LEFT JOIN ".TABLE_PREFIX."threads t ON(t.firstpost=p.pid)
    WHERE p.fid = '{$fid}'
");
$audio_tags_bit = '';
while($p = $db->fetch_array($query))
{
    $audio_tags_bit .= $p['message'];
}

$keys = get_tags($audio_tags_bit);
foreach($keys as $key)
{
    $keys .= $key.', ';
}
$keys = substr($keys,0,strlen($keys)-2);


function get_tags($str)
{
    $keywords = array();
    $str=utf8_decode($str);

    //clean string from html/bbcode
    $str = html_entity_decode($str);
    $str = strip_tags($str);
    $str = preg_replace('#\[(.*?)\]#','',$str);

    //replace - with space
    $str = preg_replace('#-#',' ',$str);

    //string in lowcase
    $str = strtolower($str);



    //clean string //only a-zA-Z0-9 and spaces
    $str = preg_replace('#[^a-zA-Z0-9\s]#','',$str);



    //split by whitespace
    $splits = preg_split('#\s+#',$str);

    foreach($splits as $key)
    {

        $key=trim($key);
        if($key=="" || strlen($key) < 4 || is_numeric($key) || isset($forbidden[$key])) continue;
        if(!isset($keywords[$key]))
        {
            $keywords[$key] = 1;
        }
        else
        {
            $keywords[$key]++;
        }
    }
    //sort, reverse
    asort($keywords);
    $keywords = array_reverse($keywords);
    $tmpkeywords =  array_keys(array_slice($keywords,0,$max));
    $keywords = array();
    foreach($tmpkeywords as $key)
    {
        array_push($keywords,$key);
    }
    return $keywords;
}

But the result it shows is:

Arrayquid, carum, finem, quasi, ille, sapiens, itaque, semper, constructio, interrete, vacabit, reges, verum, etiam, quemque, sibi, quae, dixisset, modo, aiat, neget, elit, hanc, adipiscing, consectetur, ipsum, dolor, amet, ergo, intuens, curem, lorem, absolvere, signum, debet, institutum, illud, vehementer, esse, vexat, immo, more, stoicorum, enim, ista, lenius, alio, genere, maxime, natura, vellet, erat, satis, nondum, autem, explanatum, aliquando, faciamus, redderet, sextus, peducaeus, fortasse, epicurus, quidem, reddes, ipse, censes, latino, paucis, haec, additis, requiras, quod, fore, igitur, inquit

Notice the Array in the beginning of the result. Where it comes from? I want the result should be like:

quid, carum, finem, quasi, ille, sapiens, itaque, semper, constructio, interrete, vacabit, reges, verum, etiam, quemque, sibi, quae, dixisset, modo, aiat, neget, elit, hanc, adipiscing, consectetur, ipsum, dolor, amet, ergo, intuens, curem, lorem, absolvere, signum, debet, institutum, illud, vehementer, esse, vexat, immo, more, stoicorum, enim, ista, lenius, alio, genere, maxime, natura, vellet, erat, satis, nondum, autem, explanatum, aliquando, faciamus, redderet, sextus, peducaeus, fortasse, epicurus, quidem, reddes, ipse, censes, latino, paucis, haec, additis, requiras, quod, fore, igitur, inquit

i.e. Array should not be there. Please help

4
  • Either that property was an array that you used as a string, or you're looking at a var_dump that just says "Array" because that's the type of the thing you're dumping. Commented Dec 30, 2013 at 22:36
  • @m59 - but if it was a vardump it wouldn't be formatted that way most likely. Commented Dec 30, 2013 at 22:37
  • Is the "array" in the keywords list when you get to "asort"? Because you seem to be doing a lot of stuff down there that appears excessive/un-needed. Commented Dec 30, 2013 at 22:39
  • before the loop, but after you declare $keywords = array(); can you post the vardump() results of $keyword? Commented Dec 30, 2013 at 22:41

1 Answer 1

1

Change this:

$keys = get_tags($audio_tags_bit);
foreach ($keys as $key) {
    $keys .= $key.', ';
}

to this:

$keywords = get_tags($audio_tags_bit);
foreach ($keywords as $key) {
    $keys .= $key.', ';
}

You're actually assigning the same variable inside the foreach loop like the one you used for the fetched results

Sign up to request clarification or add additional context in comments.

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.