Php代码
代码如下 | |
* $sourceArr 原来的数组 * $key 主键 * $parentKey 与主键关联的父主键 * $childrenKey 生成的孩子的键名 * */ function arrayToTree($sourceArr, $key, $parentKey, $childrenKey) { $tempSrcArr = array(); foreach ($sourceArr as $v) { $tempSrcArr[$v[$key]] = $v; } $i = 0; $count = count($sourceArr); for($i = ($count - 1); $i >=0; $i--) { if (isset($tempSrcArr[$sourceArr[$i][$parentKey]])) { $tArr = array_pop($tempSrcArr); $tempSrcArr[$tArr[$parentKey]][$childrenKey] = (isset($tempSrcArr[$tArr[$parentKey]][$childrenKey]) && is_array($tempSrcArr[$tArr[$parentKey]][$childrenKey])) ? $tempSrcArr[$tArr[$parentKey]][$childrenKey] : array(); array_push ($tempSrcArr[$tArr[$parentKey]][$childrenKey], $tArr); } } return $tempSrcArr; } |
Php代码
* 将数组转换成树
* 例子:将 array(
array('id'=>1,'parentId' => 0,'name'=> 'name1')
,array('id'=>2,'parentId' => 0,'name'=> 'name2')
,array('id'=>4,'parentId' => 1,'name'=> 'name1_4')
,array('id'=>15,'parentId' => 1,'name'=> 'name1_5')
);转换成
* Array(
[1] => Array([id] => 1
[parentId] => 0
[name] => name1
[children] => Array(
[0] => Array([id] => 15,[parentId] => 1,[name] => name1_5)
[1] => Array([id] => 4,[parentId] => 1,[name] => name1_4)
)
)
[2] => Array([id] => 2,[parentId] => 0,[name] => name2)
)
* @param array $sourceArr 要转换的数组
* @param string $key 数组中确认父子的key,例子中为“id”
* @param string $parentKey 数组中父key,例子中为“parentId”
* @param type $childrenKey 要在树节点上索引子节点的key,例子中为“children”
* @return array 返回生成的树
*/
代码如下 | |
function arrayToTree($sourceArr, $key, $parentKey, $childrenKey) { $tempSrcArr = array(); $allRoot = TRUE; foreach ($sourceArr as $v) { $isLeaf = TRUE; foreach ($sourceArr as $cv ) { if (($v[$key]) != $cv[$key]) { if ($v[$key] == $cv[$parentKey]) { $isLeaf = FALSE; } if ($v[$parentKey] == $cv[$key]) { $allRoot = FALSE; } } } if ($isLeaf) { $leafArr[$v[$key]] = $v; } $tempSrcArr[$v[$key]] = $v; } if ($allRoot) { return $tempSrcArr; } else { unset($v, $cv, $sourceArr, $isLeaf); foreach ($leafArr as $v) { if (isset($tempSrcArr[$v[$parentKey]])) { $tempSrcArr[$v[$parentKey]][$childrenKey] = (isset($tempSrcArr[$v[$parentKey]][$childrenKey]) && is_array($tempSrcArr[$v[$parentKey]][$childrenKey])) ? $tempSrcArr[$v[$parentKey]][$childrenKey] : array(); array_push ($tempSrcArr[$v[$parentKey]][$childrenKey], $v); unset($tempSrcArr[$v[$key]]); } } unset($v); return arrayToTree($tempSrcArr, $key, $parentKey, $childrenKey); } } |
Php代码
/**递归方法:**/
$rows = array(
0 => array('id' => 1, 'name' => '菜单1', 'parentId' => 0)
, 1 => array('id' => 2, 'name' => '菜单2', 'parentId' => 0)
, 2 => array('id' => 3, 'name' => '菜单3', 'parentId' => 0)
, 3 => array('id' => 4, 'name' => '菜单1_1', 'parentId' => 1)
, 4 => array('id' => 5, 'name' => '菜单1_2', 'parentId' => 1)
, 5 => array('id' => 6, 'name' => '菜单2_1', 'parentId' => 2)
);
print_r(getTree($rows, 0, 'id', 'parentId'));
代码如下 | |
/** * 数组根据父id生成树 * @staticvar int $depth 递归深度 * @param array $data 数组数据 * @param integer $pid 父id的值 * @param string $key id在$data数组中的键值 * @param string $chrildKey 要生成的子的键值 * @param string $pKey 父id在$data数组中的键值 * @param int $maxDepth 最大递归深度,防止无限递归 * @return array 重组后的数组 */ function getTree($data, $pid = 0, $key = 'id', $pKey = 'parentId', $childKey = 'child', $maxDepth = 0){ static $depth = 0; $depth++; if (intval($maxDepth) { $maxDepth = count($data) * count($data); } if ($depth > $maxDepth) { exit("error recursion:max recursion depth {$maxDepth}"); } $tree = array(); foreach ($data as $rk => $rv) { if ($rv[$pKey] == $pid) { $rv[$childKey] = getTree($data, $rv[$key], $key, $pKey, $childKey, $maxDepth); $tree[] = $rv; } } return $tree; } |
一个实例
代码如下 | 复制代码 |
// 树组的顺序即是分类的顺序,因此前当分类的下级子类一定要紧随其后 100 => array('id'=>100, 'cname'=>'特意加进去的二级分类', 'pid'=>1), 2 => array('id'=>2, 'cname'=>'二级分类', 'pid'=>1), // 指定分类ID,返回子类量(不进行深度递归)
// 指定分类ID,www.111cn.net并返回数组(不进行深度递归)
function getTreeHTML($tree,$level = 0)
$html .= ' if (getChildTotal($node['id'])) { $tree_last = getChildArray($node['id']); $html .= ' } } } return $html; } $html = getTreeHTML( getChildArray(0) ); ';
';
echo $html; echo ' ?> |

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6
视觉化网页开发工具