如何从数组中获取过滤的字母数字值,例如范围之间的字母数字值。
$array1 = array("F6B10471", "A6A10471", "B6C10471", "D6A10471");
这是我的数组我将把 A6A10471 之间的范围传递给 D6A10471 我的预期结果为Array([0] => A6A10471[1] => B6C10471[2] => D6A10471)
答案 0 :(得分:1)
您可以使用base_convert来完成此操作。将基数从36
转换为10
并按此检查
<?php
$arr = array("F6B10471", "A6A10471", "B6C10471", "D6A10471");
$start = base_convert("A6A10471",36,10);
$end = base_convert("D6A10471",36,10);
$new = array();
foreach($arr as $val){
$value = base_convert($val,36,10);
if($value >= $start && $value <= $end ){
$new[] = $val;
}
}
print_r($new);
?>
输出:
Array
(
[0] => A6A10471
[1] => B6C10471
[2] => D6A10471
)
答案 1 :(得分:1)
将array_filter()与过滤器类一起使用的示例,如果要实现Strategy Design Pattern
,可以轻松更新<?php
class FilterBetween
{
private $start;
private $end;
public function __construct($start, $end)
{
$this->start = $start;
$this->end = $end;
}
public function filter($value)
{
$result = false;
if ($value >= $this->start && $value <= $this->end) {
$result = true;
}
return $result;
}
}
$arr = array("F6B10471", "A6A10471", "B6C10471", "D6A10471");
$objFilterBetween = new FilterBetween('A6A10471', 'D6A10471');
$arrResult = array_filter($arr, [$objFilterBetween, 'filter']);
var_dump($arrResult);