PHP注意:数组列表

时间:2015-08-23 10:02:10

标签: php

我有这个功能用于显示标签列表:

$DB_QUERY = Access::FETCH("SELECT name, " . NEWS_TAGS . ".id,tags_id,content_id  FROM " . NEWS_TAGS . " JOIN " . NEWS_POSTS_TAGS . " ON 
   " . NEWS_TAGS . ".id = " . NEWS_POSTS_TAGS . ".tags_id WHERE " . NEWS_POSTS_TAGS . ".content_id = ? AND " . NEWS_TAGS . ".type = ? ORDER BY " . NEWS_TAGS . ".name ASC LIMIT 8 ", filter_var($id, FILTER_VALIDATE_INT) , $type);
$ret = array();

foreach($DB_QUERY as $row)
    {
    $ret[] = $row['name'];
    }

$tags = '<li class="label"><a href="#"><i class="fa fa-tag"></i>' . $ret . '</a></li>';

echo $tags;

但是我看到了这个错误:

  

注意:第449行的C:\ xampp \ htdocs \ frontcms \ class \ functions.php中的数组到字符串转换

编辑:

我使用PHP print_r($DB_QUERY)打印结果print_r

Array(
    [0] => Array(
        [name] => 321[id] => 393[tags_id] => 393[content_id] => 244
    ) [1] => Array(
        [name] => tagname1[id] => 395[tags_id] => 395[content_id] => 244
    ) [2] => Array(
        [name] => tagname2[id] => 394[tags_id] => 394[content_id] => 244
    ) [3] => Array(
        [name] => tagname3[id] => 396[tags_id] => 396[content_id] => 244
    )
)

如何解决这个问题?!

2 个答案:

答案 0 :(得分:1)

$ret是一个数组,您可以在$ tags中使用它。你可以做例如

$tags = '<li class="label"><a href="#"><i class="fa fa-tag"></i>' . implode(", ", $ret) . '</a></li>';

将所有名称分隔为“,”。

但我猜你真正想做的是:

$tags = "";

foreach($DB_QUERY as $row)
    {
    $tags .= '<li class="label"><a href="#"><i class="fa fa-tag"></i>' .  $row['name'] . '</a></li>';
    }

echo $tags;

答案 1 :(得分:1)

您无需定义新阵列。您可以直接从foreach语句中回显结果:

$DB_QUERY = Access::FETCH("SELECT name, " . NEWS_TAGS . ".id,tags_id,content_id  FROM " . NEWS_TAGS . " JOIN " . NEWS_POSTS_TAGS . " ON 
   " . NEWS_TAGS . ".id = " . NEWS_POSTS_TAGS . ".tags_id WHERE " . NEWS_POSTS_TAGS . ".content_id = ? AND " . NEWS_TAGS . ".type = ? ORDER BY " . NEWS_TAGS . ".name ASC LIMIT 8 ", filter_var($id, FILTER_VALIDATE_INT) , $type);

foreach($DB_QUERY as $row) {
    echo '<li class="label"><a href="#"><i class="fa fa-tag"></i>' . $row['name'] . '</a></li>';
}