Php 根据数字模式过滤数字数组
有一个数字数组:Php 根据数字模式过滤数字数组,php,arrays,repeat,Php,Arrays,Repeat,有一个数字数组: $list = array ( [0] => 111 [1] => 112 [2] => 113 [3] => 114 [4] => 121 [5] => 122 [6] => 123 [7] => 124 [8] => 131 [9] => 132 [10] => 1234 [11] => 1123 [
$list = array
(
[0] => 111
[1] => 112
[2] => 113
[3] => 114
[4] => 121
[5] => 122
[6] => 123
[7] => 124
[8] => 131
[9] => 132
[10] => 1234
[11] => 1123
[12] => 1223
[13] => 1233
[14] => 4321
)
和一个变量(模式):
我想用以下规则过滤数组。让$list[$i]
作为$list
数组的一项,$d
作为$list[$i]
的一位数字。然后
- 如果
中等于$list[$i]
的位数大于$d
中等于$input
的位数,则必须跳过数组项李>$d
- 如果在
中未指定$input
位,则必须跳过数组项$d
$input
变量中
出现两次1
和2
出现一次3
$list = array
(
[0] => 111 ==> should be removed (1 is only defined twice in $input, so it shouldn't appear more than twice)
[1] => 112
[2] => 113
[3] => 114 ==> should be removed (there is no 4)
[4] => 121
[5] => 122 ==> should be removed (2 is only defined once, so it shouldn't appear more than once)
[6] => 123
[7] => 124 ==> should be removed (there is no 4)
[8] => 131
[9] => 132
[10] => 1234 ==> should be removed (there is no 4)
[11] => 1123
[12] => 1223 ==> should be removed (2 is only defined once in $input, so it shouldn't appear more than once)
[13] => 1233 ==> should be removed (3 is only defined once in $input, so it shouldn't appear more than once)
[14] => 4321 ==> should be removed (there is no 4)
)
如何实现这一点?请告诉我它是否有效
For that you can check count of array by splinting and array_unique methode of PHP.
$main_arr = array(111,112,113,114,121,122,123,124,131,132,1234,1123,1223,1233,4321);
$ans = [];
foreach ($main_arr as $value){
$sub_arr = str_split($value);
if(count($sub_arr) == count(array_unique($sub_arr))){
array_push($ans,$value);
}
}
print_r($ans);
This will output:
Array (
[0] => 123
[1] => 124
[2] => 132
[3] => 1234
[4] => 4321
)
Thanks
$input = 1123;
$output = array(111,112,113,114,121,122,123,124,131,132, 1234, 1123, 1223, 1233, 4321);
$count = count_chars($input, 1);
$result = array_filter($output, function($n) use($input, $count) {
foreach(count_chars($n, 1) as $i => $val) {
if(strpos($input, $i) === false) {
return 0;
}else if($val > $count[$i]){
return 0;
}
}
return 1;
});
为什么11111 2。。应该被删除?你可以尝试一些东西,为什么在世界上
111
应该被删除而123
保留?这背后的逻辑是什么。你需要告诉我们原因,而不仅仅是你想要的结果。@MuhammedImranHussain和Irvin,因为我认为这是重复的。111是一个三元组。112是双人的。它多次包含数字1:)我认为它是数字中的重复数字OP希望删除$input
变量中给出的重复数字,而不是所有具有重复数字的条目。警告错误消息:strpos()预期参数1为stringwhere$input declaredGot现在:警告错误消息:为foreach()提供的参数无效我使用了我的变量,请粘贴您的代码好吗?@oitoit我想您已经更新了基于输入字母计数的要求元素可以包含那么多重复。例如,如果输入包含11,则在数组1中可以接受两次。我将更新我的代码。首先,确认代码正常工作
$input = 1123;
$output = array(111,112,113,114,121,122,123,124,131,132, 1234, 1123, 1223, 1233, 4321);
$count = count_chars($input, 1);
$result = array_filter($output, function($n) use($input, $count) {
foreach(count_chars($n, 1) as $i => $val) {
if(strpos($input, $i) === false) {
return 0;
}else if($val > $count[$i]){
return 0;
}
}
return 1;
});
// Prepare counters for the digits in $input
foreach (str_split((string)$input) as $d)
@$counters[$d]++;
$result = [];
foreach ($list as $key => $n) {
// Counter for digits in $n
$tmp_counter = [];
foreach (str_split((string)$n) as $d) {
// $d is not specified in $input, so skip $n
if (empty($counters[$d]))
continue 2;
// The number of $d entries in $n is greater than
// it is specified in $input, so skip $n
if (@++$tmp_counter[$d] > $counters[$d])
continue 2;
}
$result[$key] = $n;
}