时间:2021-07-01 10:21:17 帮助过:25人阅读
请给出测试数据
get 提交 $keyword数据为 25*50
经过下面方法得出的结果也显示50*20 但是继续搜索25*50*2.5 就只会出现25*50*2.5 不会出现50*25*2.5
$cArr = array();
if(!empty($keyword)){
$cArr['keyword'] = $keyword;
}
require_once('/config.search.php');
$skeys=array_keys($s_replaceArr);
if(@$s_replaceArr[$keyword])
{
$cArr['keyword'] =$s_replaceArr[$keyword]['single'];
}else{
$eregpatt=implode('|',$skeys);
preg_match_all("/^($eregpatt)/", $keyword,$res);
//print_r($res);
if($res[1]){
$keywordArr=array();
foreach($s_replaceArr[$res[1][0]]['mixed'] as $svalue)
{
$keywordArr[]=preg_replace("/{$res[1][0]}/",$svalue,$keyword,1);
}
$cArr['keyword']=implode(" ",$keywordArr);
}
}
config.search.php数据为
$s_replaceArr=Array(
'20*40' => array(
'single'=>'20*40 40*20',
'mixed'=>array('20*40','40*20')
),
'40*20' => array(
'single'=>'40*20 20*40',
'mixed'=>array('20*40','40*20')
),
'25*50' => array(
'single'=>'25*50 50*25',
'mixed'=>array('25*50','50*25')
),
'50*25' => array(
'single'=>'50*25 25*50',
'mixed'=>array('25*50','50*25')
)
);
?>
我25*50后面还有很多组合,真不想挨个写到config.search.php 有什么好办法解决 谢谢版主了
你可能要的是这个
$key = '20*50*30';$arr = explode('*', $key);print_r(Arrangement($arr));//排列 Arrangementfunction Arrangement($arr = array(), $res = '') { if(! is_array($arr) ) $arr = str_split($arr); if(empty($arr)) $array[] = $res; else foreach($arr AS $k => $v) { unset($arr[$k]); foreach( Arrangement($arr, trim("$res*$v", '*')) AS $t) $array[] = $t; $arr[$k] = $v; } return $array;}
Array( [0] => 20*50*30 [1] => 20*30*50 [2] => 50*30*20 [3] => 50*20*30 [4] => 30*20*50 [5] => 30*50*20)
版主就是版主 写的非常好
最后一个问题 我如何接受到这个数组
$arr=Array( "[0]" => "20*50*30", "[1]" => "20*30*50", "[2]" => "50*30*20", "[3]" => "50*20*30", "[4]" => "30*20*50", "[5]" => "30*50*20", "[6]" => "25*50*25");$key = "25*50*2.5";$arr = explode('*', $key);//print_r(Arrangement($arr)); //排列 Arrangementfunction Arrangement($arr = array(), $res = '') { if(! is_array($arr) ) $arr = str_split($arr); if(empty($arr)) $array[] = $res; else foreach($arr AS $k => $v) { unset($arr[$k]); foreach( Arrangement($arr, trim("$res*$v", '*')) AS $t) $array[] = $t; $arr[$k] = $v; } return $array;}
Array ( [0] => 25*50*2.5 [1] => 25*2.5*50 [2] => 50*2.5*25 [3] => 50*25*2.5 [4] => 2.5*25*50 [5] => 2.5*50*25 ) 变成:$cArr['keyword'] = 25*50*2.5 25*2.5*50 50*2.5*25 50*25*2.5 2.5*25*50 2.5*50*25 ;
版主就是版主,支持一下!
$key = $keyword;$arr = explode('*', $key);//print_r(Arrangement($arr)); //排列 Arrangementfunction Arrangement($arr = array(), $res = '') { if(! is_array($arr) ) $arr = str_split($arr); if(empty($arr)) $array[] = $res; else foreach($arr AS $k => $v) { unset($arr[$k]); foreach( Arrangement($arr, trim("$res*$v", '*')) AS $t) $array[] = $t; $arr[$k] = $v; } return $array; }$aarray = Arrangement($arr);$cArr['keyword']=implode(" ",$aarray);echo $cArr['keyword'];