php将字符串数组与字符串进行比较
假设我有一根绳子php将字符串数组与字符串进行比较,php,arrays,compare,Php,Arrays,Compare,假设我有一根绳子 $string = "12315"; 我要比较此数组,并希望结果如下: $arr = [ "123", //should be true "115", //should be true "111", //false "132", //true "1512" //true "5531" //false ] 数组值的每个计数都不应大于给定的字符串 我该怎么做?提前谢谢你 使用数组映射功能: foreach($array as $strin
$string = "12315";
我要比较此数组,并希望结果如下:
$arr = [
"123", //should be true
"115", //should be true
"111", //false
"132", //true
"1512" //true
"5531" //false
]
数组值的每个计数都不应大于给定的字符串
我该怎么做?提前谢谢你 使用
数组映射
功能:
foreach($array as $string)
{
if(strpos($searchstring, $string) !== false)
{
echo 'yes its in here';
break;
}
}
$string = '...';
$arr = ['..', '.', '..,', '....']; // true, true, false, false
function check($str) {
/* Implement your check here */
/* Smthng like: */
$a = $string;
foreach ($i = 0; $i<strlen($str); $i++) {
$index = strpos($a, $str[$i]);
if ($index === false) return false;
else substr_replace($a,'', $index, 1);
}
return true;
}
$result = array_map(check, $arr);
$string='…';
$arr=['…',','…','…'];//真,真,假,假
函数检查($str){
/*在这里兑现你的支票*/
/*类似于:*/
$a=$string;
foreach($i=0;$i使用array\u map
函数:
$string = '...';
$arr = ['..', '.', '..,', '....']; // true, true, false, false
function check($str) {
/* Implement your check here */
/* Smthng like: */
$a = $string;
foreach ($i = 0; $i<strlen($str); $i++) {
$index = strpos($a, $str[$i]);
if ($index === false) return false;
else substr_replace($a,'', $index, 1);
}
return true;
}
$result = array_map(check, $arr);
$string='…';
$arr=['…',','…','…'];//真,真,假,假
函数检查($str){
/*在这里兑现你的支票*/
/*类似于:*/
$a=$string;
foreach($i=0;$i首先创建一个可能的组合,然后进行比较
试试这个
function create_possible_arrays(&$set, &$results)
{
for ($i = 0; $i < count($set); $i++)
{
$results[] = $set[$i];
$tempset = $set;
array_splice($tempset, $i, 1);
$tempresults = array();
create_possible_arrays($tempset, $tempresults);
foreach ($tempresults as $res)
{
$results[] = $set[$i] . $res;
}
}
}
$results = array();
$str = '12315'; //your input string
$str = str_split($str); //create array
create_possible_arrays($str, $results);
$inputs = array(
"123", //should be true
"115", //should be true
"111", //false
"132", //true
"1512", //true
"5531"
);
foreach($inputs as $input){
if(in_array($input,$results)){
echo $input.' true <br/>';
}else{
echo $input.' false <br/>';
}
}
your result:
123 true
115 true
111 false
132 true
1512 true
5531 false
函数创建可能的数组(&$set,&$results)
{
对于($i=0;$i ”;
}否则{
echo$input。“false
”;
}
}
您的结果:
123正确
115对
111错误
132对
1512对
5531假
首先创建一个可能的组合,然后进行比较
试试这个
function create_possible_arrays(&$set, &$results)
{
for ($i = 0; $i < count($set); $i++)
{
$results[] = $set[$i];
$tempset = $set;
array_splice($tempset, $i, 1);
$tempresults = array();
create_possible_arrays($tempset, $tempresults);
foreach ($tempresults as $res)
{
$results[] = $set[$i] . $res;
}
}
}
$results = array();
$str = '12315'; //your input string
$str = str_split($str); //create array
create_possible_arrays($str, $results);
$inputs = array(
"123", //should be true
"115", //should be true
"111", //false
"132", //true
"1512", //true
"5531"
);
foreach($inputs as $input){
if(in_array($input,$results)){
echo $input.' true <br/>';
}else{
echo $input.' false <br/>';
}
}
your result:
123 true
115 true
111 false
132 true
1512 true
5531 false
函数创建可能的数组(&$set,&$results)
{
对于($i=0;$i ”;
}否则{
echo$input。“false
”;
}
}
您的结果:
123正确
115对
111错误
132对
1512对
5531假
试试这个:
试试这个:
请解释如何将'12315'
与'123'
的数组值进行比较,'115'
应计算为true
,而与'111'
的比较应计算为false
@someOne,因为字符串“12315”包含两个数字“1”,而“111”是三个数字“1”,所以它应该返回FALSE(我想我应该在这种情况下使用regex?regex?哪种方式?这绝对是非常规任务)您试图解决的真正任务是什么?请解释如何将'12315'
与'123'
的数组值进行比较,'115'
应计算为true
,而与'111'
的比较应计算为false
@someOne,因为字符串“12315”包含两个数字“1”,而“111”是“1”的三个数字,所以它应该返回false我想我应该在这种情况下使用regex?regex?哪种方式?它绝对是非常规任务)你试图解决什么真正的任务?当我更改$current_user_can_see=explode(“,”,“1,3,6,10,20,1,1”),它仍然将$show显示为“1,6,10”,我希望它不显示(false)您也可以使用这种方式“$match\u index=array\u search($matches[0],$array);”查看此链接,当我更改$current\u user\u can\u see=explode(“,”,“1,3,6,10,20,1,1”),它仍然将$show显示为“1,6,10”,我希望它不显示(false)。您也可以使用这种方式“$match\u index=array\u search($matches[0],$array);"看到这个链接,你可能会有想法是的,萨提斯,你是我的救命恩人!简单而优雅的回答!也感谢其他人帮我这么远!:)是的,萨提斯,你是我的救命恩人!简单而优雅的回答!也感谢其他人帮我这么远!:)你的回答也是正确的!你能允许我接受两个答案吗?你的答案也是正确的!我能接受两个答案吗?不是真的正确,1512也应该返回(真)不是真的正确,1512也应该返回(真)