PHP+mysql 例如我想搜索关键词 $keyword为25*50的结果中同时显示25*50及50*25的结果,然后继续搜索25*50*2.5的结果中也同时显示50*25*2.5的结果。(后面还有25*50*1.1到25*50*4.75)
需要PHP进行怎样的判断或者方法。
请给出测试数据
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'];