<?php
$tags = '[{"TagId":"5297739878417567745","TagName":"器械","Icon":"","TagPid":"0","TagPath":"\/","Selected":0,"Child":[{"TagId":"5297739991856713729",
"TagName":"瑜伽球","Icon":"","TagPid":"5297739878417567745","TagPath":"\/5297739878417567745","Selected":1,"Child":[]},{"TagId":"5297739939595685889",
"TagName":"哑铃","Icon":"","TagPid":"5297739878417567745","TagPath":"\/5297739878417567745","Selected":1,"Child":[]}]},
{"TagId":"5285677198231998465","TagName":"课件类型标签","Icon":"","TagPid":"0","TagPath":"\/","Selected":0,
"Child":[{"TagId":"5285678117011066881","TagName":"训练目的","Icon":"","TagPid":"5285677198231998465","TagPath":"\/5285677198231998465",
"Selected":0,"Child":[{"TagId":"5304964691385126913","TagName":"强化躯干力量和核心力量","Icon":"","TagPid":"5285678117011066881",
"TagPath":"\/5285677198231998465\/5285678117011066881","Selected":1,"Child":[]}]}]}]';
/**
* 根据config算法匹配标签
* depth : 匹配节点所在深度。
* matchMethod : 匹配方式。
* name : 匹配字符
* show : 是否展示全路径,check :只显示选中一个节点,checkAll:显示所有路径上的节点(从depth 深度 节点开始)
*/
* 'app_tags'=>[
'queue' => [['depth'=>1,'matchMethod' => 'part','name'=>'队列','show'=>'checkAll']],
'target' => [['depth'=>1,'matchMethod' => 'part','name'=>'训练目的','show'=>'check']],
'instrument' => [['depth'=>0,'matchMethod' => 'part','name'=>'器械','show'=>'checkAll']],
'showTag' => [
['depth'=>1,'matchMethod' => 'part','name'=>'心率','show'=>'checkAll'],
['depth'=>1,'matchMethod' => 'part','name'=>'消耗','show'=>'checkAll'],
['depth'=>1,'matchMethod' => 'part','name'=>'适用水平','show'=>'checkAll'],
['depth'=>1,'matchMethod' => 'part','name'=>'强度','show'=>'checkAll'],
['depth'=>1,'matchMethod' => 'part','name'=>'类别','show'=>'checkAll'],
],
],
* @param [type] $tags
* @return void
* @Author lianghuiju@aocyun.com
* @DateTime 2022-01-12
*/
public static function matchTags($tags,$matchKey = 'app_tags', $join = true, $joinStr = '、')
{
$result = [];
$algorithm = config($matchKey) ?? [];
if($algorithm){
foreach($algorithm as $key => $al){
$result[$key] = [];
$ret = [];
$match = false;
$depth = 0;
$ret = self::recursion_tags($tags,$al,$depth,$ret,$match);
if($join && is_array($ret)){
foreach($ret as $a=>$r){
$ret[$a] = implode($joinStr,$r);
}
}
if(is_array($ret)) $ret = array_values($ret);
$result[$key] = $ret;
}
}
return $result;
}
/**
* 递归调用
*
* @param [type] $tags
* @param [type] $algorithm
* @param [type] $depth
* @param [type] $result
* @param boolean $match
* @return void
* @Author lianghuiju@aocyun.com
* @DateTime 2022-01-12
*/
public static function recursion_tags($tags, $algorithm, $depth, $result, $match = false, $matchName = false)
{
foreach($tags as $k => $tag){
$ret = false;
$matchMethod = '';
$name = '';
$show = '';
$dep = '';
$mName = $matchName;
$algo = $algorithm;
foreach($algorithm as $k=>$al){
$matchMethod = $al['matchMethod'];
$name = $al['name'];
$show = $al['show'];
$dep = $al['depth'];
if( $depth == $dep){
if($matchMethod == 'part'){
$ret = strstr($tag['TagName'],$name) === false ? false : true;
}else if($matchMethod == 'all'){
$ret = strcmp($tag['TagName'],$name) === 0 ? true : false;
}
}
if($ret) {
$match = true;
$algo = [$al];
$mName = $k;
break;
}
}
if( $match && $mName !== false && ( ($show == 'check' && $tag['Selected'])
|| ($show == 'checkAll') ) )
$result[$mName][] = $tag['TagName'];
if( !empty($tag['Child']) ){
$result = self::recursion_tags($tag['Child'], $algo, $depth + 1, $result, $match, $mName);
}
}
return $result;
}
神回复
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。