你能清理Print_R输出吗?

时间:2017-10-25 19:29:43

标签: php joomla3.0

我是从print_r输出中得到的:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => http://www.ls.bouwmanbv.nl [1] => http://www.ls.bouwmanbv2.nl [2] => http://www.ls.bouwmanbv3.nl [3] => http://www.ls.bouwmanbv4.nl ) 

如何让它跳过空Array ()并删除[#] =>,以便我得到一个这样的列表:

http://www.ls.bouwmanbv.nl
http://www.ls.bouwmanbv2.nl
http://www.ls.bouwmanbv3.nl
http://www.ls.bouwmanbv4.nl

这是有问题的代码:

<?php
$user_  = JFactory::getUser();
$db     = JFactory::getDBO();
$levels = JAccess::getAuthorisedViewLevels($user->id);

foreach($levels as $key => $level)
{
  $query = $db->getQuery(true);
  $query  = 'SELECT title FROM #__pf_projects';
  $query .= ' WHERE access = ' . $level;
  $db->setQuery($query);
  $results = $db->loadColumn();
  print_r($results);
}

感谢大家的帮助。

2 个答案:

答案 0 :(得分:2)

我相信您正在尝试echo而不是print_r,因为您可以使用以下内容:

foreach($levels as $key => $level)
{
    $query = $db->getQuery(true);
    $query  = 'SELECT title FROM #__pf_projects';
    $query .= ' WHERE access = ' . $level;
    $db->setQuery($query);
    $results = $db->loadColumn();

    foreach($results as $result)
    {
        echo $result;
    }
}

答案 1 :(得分:0)

$user_  = JFactory::getUser();
$db     = JFactory::getDBO();
$levels = JAccess::getAuthorisedViewLevels($user->id);

foreach($levels as $key => $level)
{
    $query = $db->getQuery(true);
    $query    = 'SELECT title FROM #__pf_projects';
    $query .= ' WHERE access = ' . $level;
    $db->setQuery($query);
    $results = $db->loadColumn();
    if(empty($results)) continue;
    foreach($results as $result) {
        echo $result;
    }
}

您可以使用if(empty($results)) continue;跳过空数组,然后使用foreach循环列出数组内容。