<?php
$arr = array(array(1,2), array(3, 4), array(array(5, 6), array(7, 8)));
function t($a)
{
if(is_array($a))
{
foreach($a as $k=>$v)
{
$a[$k] = t($v);
}
}else{
$a = $a*10;
}
return $a;
}
$ab = t($arr);
// 此时数组的每个元素都乘了 10 了
print_r($ab);
// 结果为
Array ( [0] => Array ( [0] => 10 [1] => 20 ) [1] => Array ( [0] => 30 [1] => 40 ) [2] => Array ( [0] => Array ( [0] => 50 [1] => 60 ) [1] => Array ( [0] => 70 [1] => 80 ) ) )
=======================
如下示例:
function test($n) {
echo $n . " ";
if($n > 0)
{
test($n - 1);
}else
{
echo " <--> ";
}
echo " || ";
echo $n . " ";
}
test(6);
结果如下:6 5 4 3 2 1 0 <--> || 0 || 1 || 2 || 3 || 4 || 5 || 6
无限分类
数据库:(fid就是父类,默认为0,即总类为 0)
$conn = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db("test", $conn);
mysql_query("set names utf8", $conn);
function get_array($id = 0) {
global $conn;
$sql = "select * from e_cate where fid = '{$id}'";
$res = mysql_query($sql, $conn);
$arr = array();
if($res)
{
while($rows = mysql_fetch_assoc($res))
{
$rows['child'] = get_array($rows['id']);
$arr[] = $rows;
}
return $arr;
}
}
$rr = get_array(0);
print_r($rr);
形式如下所示:
Array
(
[0] => Array
(
[id] => 1
[fid] => 0
[name] => 电子
[child] => Array
(
[0] => Array
(
[id] => 2
[fid] => 1
[name] => 数码
[child] => Array
(
[0] => Array
(
[id] => 3
[fid] => 2
[name] => 相机
[child] => Array
(
)
)
[1] => Array
(
[id] => 11
[fid] => 2
[name] => 手机
[child] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4
[fid] => 1
[name] => 3c
[child] => Array
(
[0] => Array
(
[id] => 5
[fid] => 4
[name] => 笔记本
[child] => Array
(
)
)
[1] => Array
(
[id] => 12
[fid] => 4
[name] => 平板
[child] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 6
[fid] => 0
[name] => 服装
[child] => Array
(
[0] => Array
(
[id] => 7
[fid] => 6
[name] => 女装
[child] => Array
(
[0] => Array
(
[id] => 8
[fid] => 7
[name] => 谱子
[child] => Array
(
)
)
[1] => Array
(
[id] => 13
[fid] => 7
[name] => 裙子
[child] => Array
(
)
)
)
)
[1] => Array
(
[id] => 9
[fid] => 6
[name] => 男装
[child] => Array
(
[0] => Array
(
[id] => 10
[fid] => 9
[name] => 西装
[child] => Array
(
)
)
[1] => Array
(
[id] => 14
[fid] => 9
[name] => 皮鞋
[child] => Array
(
)
)
)
)
)
)
)
================
如果要把上面的无限分类格式化,如图所示不:
格式化函数:
function procHtml($tree)
{
$html = '';
foreach($tree as $t)
{
if($t['child'] == '')
{
$html .= "<li>{$t['name']}</li>";
}else
{
$html .= "<li>" . $t['name'];
$html .= procHtml($t['child']);
$html = $html . "</li>";
}
}
return $html ? "<ul>" . $html . "</ul>" : $html;
}
echo procHtml($rr);
如果再加上样式,即可变成自己的样式了
===============
其实可以直接用以下方法即可,减少了与数据库的交互
/* 大概步骤如下: 首先到数据库取数据,放到一个数组, 然后把数据转化为一个树型状的数组, 最后把这个树型状的数组转为html代码。
*/
$conn = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db("test", $conn);
mysql_query("set names utf8", $conn);
$sql = "select * from e_cate";
$arr = array();
$res = mysql_query($sql, $conn);
if($res)
{
while($rows = mysql_fetch_assoc($res))
{
$arr[] = $rows;
}
}
// 把上面数组转换成树形,保存在数组中,不用再去查询数库存了
function getTree($data, $fid)
{
$tree = '';
foreach($data as $k => $v)
{
if($v['fid'] == $fid)
{
//父亲找到儿子
$v['child'] = getTree($data, $v['id']);
$tree[] = $v;
}
}
return $tree;
}
$tree = getTree($arr, 0);
//把树型状数组转为html
function procHtml($tree)
{
$html = '';
foreach($tree as $t)
{
if($t['child'] == '')
{
$html .= "<li>{$t['name']}</li>";
}else
{
$html .= "<li>" . $t['name'];
$html .= procHtml($t['child']);
$html = $html . "</li>";
}
}
return $html ? "<ul>" . $html . "</ul>" : $html;
}
echo procHtml($tree);
//最终效果与上面的一样
=================
如果感觉上面的递归难理解,可用下面的例子:
$conn = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db("test", $conn);
mysql_query("set names utf8", $conn);
$sql = "select * from e_cate";
$array = array();
$res = mysql_query($sql, $conn);
if($res)
{
while($rows = mysql_fetch_assoc($res))
{
$array[] = $rows;
}
}
$arrs = array();
function display_tree($tag,$id)
{
global $array, $arrs;
//查询出来的数组,这里申明成常量,为了可以调用外界的$array
$result = findChild($array,$id);
//取得当前节点下的所有同级子节点
foreach ($result as $k => $v){
// 缩进显示节点名称
$arrs[] = $tag.$v['name'];
//再次调用这个函数显示子节点下的同级子节点
display_tree($tag."|_____|",$v['id']);
}
}
//取得当前节点下的所有同级子节点
function findChild($arr,$id){
$childs=array();
foreach ($arr as $k => $v){
if($v['fid']== $id){
$childs[]=$v;
}
}
return $childs;
}
display_tree("",0);
//初次显示树中的根节点
foreach($arrs as $k => $v) {
print_r($v); echo "<br />";
}