热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

子项和父项会是同一项?$term_id==$child?

后端开发|php教程子项和父项会是同一项?$term_id$child?后端开发-php教程get_term_children函数里:子项和父项会是同一项???$term_id$c

后端开发|php教程子项和父项 会是同一项? $term_id == $child ?
子项和父项 会是同一项? $term_id == $child ?
后端开发-php教程
get_term_children 函数里:
子项和父项 会是同一项??? $term_id == $child ???
以下为源码,困惑很久了,请高手指点一二。
/**
* Merge all term children into a single array of their IDs.
*
* This recursive function will merge all of the children of $term into the same
* array of term IDs. Only useful for taxonomies which are hierarchical.
*
* Will return an empty array if $term does not exist in $taxonomy.
*
* @since 2.3.0
*
* @param string $term_id ID of Term to get children.
* @param string $taxonomy Taxonomy Name.
* @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
*/
function get_term_children( $term_id, $taxonomy ) {
if ( ! taxonomy_exists($taxonomy) )
return new WP_Error(‘invalid_taxonomy’, __(‘Invalid taxonomy’));
webqq登录 源码,如何还原vscode初始化,ubuntu 编译 qt,给tomcat配置jre,爬虫 网站维护,php制作登录页面,seo优化要多长时间lzw
$term_id = intval( $term_id );
火影忍者ol源码下载,ubuntu开启触屏,tomcat在哪里改连接数,如何设置爬虫环境,php中间件有什么用,老襄阳seolzw
$terms = _get_term_hierarchy($taxonomy);
商之翼app源码,vscode是用什么开发,手机 安装 ubuntu,tomcat7.exe,sqlite FROM,开平网页设计,查看虚拟主机数据库名称,阿里云服务器容易被攻击吗,网易邮箱插件,az前端框架,拨号爬虫,php开源代码,seo外包效果,springboot文件链接,js标签球,discuz发帖时主题实现链接到别的网站,易语言网页游戏辅助,企业模板中文,espcms 后台密码,完善修改个人信息jsp页面,客户管理系统(密码123456),vs的程序源文件lzw
if ( ! isset($terms[$term_id]) )
return array();

$children = $terms[$term_id];

foreach ( (array) $terms[$term_id] as $child ) {
if ( $term_id == $child ) {
continue;
}

if ( isset($terms[$child]) )////如果 子项也有子项的话
$children = array_merge($children, get_term_children($child, $taxonomy));
}

return $children;
}
附:
/**
* Retrieves children of taxonomy as Term IDs.
* 子项仅返回 term_id
* @ignore
* @since 2.3.0
*
* @param string $taxonomy Taxonomy name.
* @return array Empty if $taxonomy isn’t hierarchical or returns children as Term IDs.
*/
function _get_term_hierarchy( $taxonomy ) {
if ( !is_taxonomy_hierarchical($taxonomy) )
return array();
$children = get_option(“{$taxonomy}_children”);

if ( is_array($children) )
return $children;
$children = array();
$terms = get_terms($taxonomy, array(‘get’ => ‘all’, ‘orderby’ => ‘id’, ‘fields’ => ‘id=>parent’));
foreach ( $terms as $term_id => $parent ) {
if ( $parent > 0 )
$children[$parent][] = $term_id;
}
update_option(“{$taxonomy}_children”, $children);

return $children;
}

回复讨论(解决方案)

数据库表中每列的数据限制,并不能保证都有实际意义。
if ( $term_id == $child ) –验证数据的有效性。
通过 wordpress 后台插入的数据,不会出现这种情况。但不能保证直接在 数据库插入的数据有这种情况。

get_term_children 用于提取出所有子分类
参数
$term_id 为开始检查的分类 id
$taxonomy 为分类名称

$terms = _get_term_hierarchy($taxonomy);
按名称检索出待检查的分类 id 集合

foreach ( (array) $terms[$term_id] as $child ) {
if ( $term_id == $child ) {
continue;
}
遍历取到的分类 id 集合,并作相应操作

既然 $term_id 是分类 id,那么他出现在 分类 id 集合 中是再正常不过的事情了

从逻辑上来说,自己不能是自己的父项。楼上说得是对的。但是,我们是从数据库中取数据的,这里的数据是人们从网站后台或直接从操作数据库加入的。 if ( $term_id == $child ) —-这样的验证,对从网站后台插入的数据,是多此一举:对直接操作数据库加入的数据,或许有实际意义。
不知这样理解是否全面

因为 $terms = _get_term_hierarchy($taxonomy);
是按名称检索出待检查的分类 id 集合
所以你不能认定 $term_id 的 就一定不在 _get_term_hierarchy($taxonomy) 的结果中
因为命名和 id 并不是唯一对应的,何况 _get_term_hierarchy 还是模糊查询

答案在:function wp_insert_term( $term, $taxonomy, $args = array() ) 里—-
If the taxonomy is hierarchical, and the ‘parent’ argument is not empty,
the term is inserted and the term_id will be given.
the parent arguments 并没有要求与 the term_id 必须不相等。
弱弱的问一句:会出现相等的情况吗?如果从以下代码看,可能会出现这种情况:
foreach ( (array) $terms[$term_id] as $child ) {
if ( $term_id == $child ) {
continue;
}
不知这样理解,对不对?请指教为盼。

$terms = _get_term_hierarchy($taxonomy);
$terms 是从其他途径获取的,所以 $terms[$term_id] 中 含有 $term_id 是很正常的
他已经这样处理了
if ( $term_id == $child ) { //如果是自己
continue; //就继续循环,不做其他操作
}
所以也就没有必要继续纠结下去了

如果你只是为了想知道:什么情况下会有 $term_id == $child
你在 continue 之前 打印出 $terms 和 $taxonomy 看看就知道了

迭代,从底端返回项端
这样,理解就没有问题了。

单次你都不能理解迭代(递归)只能让你更糊涂

递归两个特征:A 函数重复调用自身 B 有出口,有初始值

你标注的出口并非真的是出口!
continue 是流程控制语句

continue 在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。

这个递归没有出口,是吧?我真找不出来

递归是有条件进行的:有子项才递归进入

依您所说,那这个 算什么?不算递归?迷惑了。请指教

递归
可以是:有条件进入(广度优先)
可以是:有条件返回(深度优先)
具体使用要根据应用场景确定

对遍历过程、循环过程不太清楚,导致越弄越糊涂。谢谢大师指点。


推荐阅读
author-avatar
平凡的淘淘不绝_587
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有