level分层次输出内容添加leve

时间:2021-09-14 03:36:03

代码如下:
function getSubComments($parent = 0, $level = 0) { 
$db = &JFactory::getDBO();

$sql = "..."; // 查询记录的SQL 
$db->setQuery($sql); 
$rows = $db->loadObjectList();

$list = array();

// 先从数据得到记录集,再对记录添加level, 父层level = 0,它的下级level = 1,如此类推 
foreach ($rows as $row) { 
$row->level = $level; 
$list[] = $row;

$tmpArr = getSubComments($row->id, $level + 1); // 递归调用 
if (count($tmpArr)) { 
foreach ($tmpArr as $tmpRow) { 
$list[] = $tmpRow; 


}

return $list; 
}

$list = array(); 
foreach ($tmpList as $row) { 
$row->level = 0; 
$list[] = $row; 
$tmpList2 = getSubComments($row->id, 1); 
foreach ($tmpList2 as $row2881064151) { 
$list[] = $row2; 

}

// 按level分层次输出内容 
if ($row->level) { 
$pre = ''; 
for ($n = 0; $n < $row->level; $n++) 
$pre .= '----';

echo $pre . '|- '; 

echo strip_tags($row->content);

代码如下:
<?php 
$a = array("a" => "apple", "b" => "banana"); 
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); 
$c = $a + $b; // Union of $a and $b 
echo "Union of \$a and \$b: \n"; 
var_dump($c); 
$c = $b + $a; // Union of $b and $a 
echo "Union of \$b and \$a: \n"; 
var_dump($c); 
?>

When executed, this script will print the following: 
Union of $a and $b: 
代码如下:
array(3) { 
["a"]=> 
string(5) "apple" 
["b"]=> 
string(6) "banana" 
["c"]=> 
string(6) "cherry" 

Union of $b and $a: 
array(3) { 
["a"]=> 
string(4) "pear" 
["b"]=> 
string(10) "strawberry" 
["c"]=> 
string(6) "cherry" 
}