我有一个从文件中读取的数组,我正在try 删除该数组中的所有可能性,但保留原始数字.

$n = Array
(
    [0] => 050
    [1] => 051
    [2] => 052
    [3] => 501
    [4] => 054
    [5] => 150
    [6] => 061
    [7] => 062
    [8] => 510
    [9] => 064
)

在我的代码中,我将文件循环到一个数组中,然后我有一个名为como的函数,它给出了该数字的所有可能组合,例如:051可以是150,510,501.

我的组合函数如下:

function combo($num){
    
    $a = array();
    $n_s = str_split($num);
    
    
    // 5 8 2
    
    // first combo
    $c1 = $n_s[0] . '' . $n_s[2] . '' . $n_s[1];
    $c2 = $n_s[1] . '' . $n_s[0] . '' . $n_s[2];
    $c3 = $n_s[1] . '' . $n_s[2] . '' . $n_s[0];
    $c4 = $n_s[2] . '' . $n_s[0] . '' . $n_s[1];
    $c5 = $n_s[2] . '' . $n_s[1] . '' . $n_s[0];
    

    array_push($a, $c1, $c2, $c3, $c4, $c5);
    
    return($a);
    
}


function mirror($n){
    
    $m = array("0" => "5", "1" => "6", "2" => "7", "3" => "8", "4" => "9", "5" => "0", "6" => "1", "7" => "2", "8" => "3", "9" => "4");
    
    
    $n_s = str_split($n);
    
    $f = $m[$n_s[0]] . '' . $m[$n_s[1]] . '' . $m[$n_s[2]];

    return $f;

}
foreach($n as $v){
        echo $v . '<br>';

        $t = combo($v);
        
        foreach($t as $vs){
            unset($n[$vs]);
        }
}

因此,我希望得到的结果是返回原始数组,保留051,并删除501,150,510个变量.这将在250个数字的循环下工作.如果我的计算是正确的,它应该返回大约包含25个数字的最终array.

任何帮助都是感激不尽的,我似乎做不好,我错过了什么.

推荐答案

我的 idea 是将051,501,150,510全部转换为015,以便可以过滤出重复项,然后使用array_intersect_key获取剩余内容的键,并使用该键从原始数组中返回值.

// Converted to string since you want to keep your leading zero?
$array = [ '050','051','052','501','054','150','061','062','510','064' ];

// sort each digit in each array item so we can apply array_unique to the entire set of numbers to remove duplicates
$reduced = array_unique( array_map( function( $n ){
    $number = str_split( $n );
    sort($number);
    return implode( '', $number );
}, $array ) );

// Use the keys from this new array to extract the values from the original array
$output = array_intersect_key( $array, $reduced );

// done!
print_r( $output );

退货

Array
(
    [0] => 050
    [1] => 051
    [2] => 052
    [4] => 054
    [6] => 061
    [7] => 062
    [9] => 064
)

Php相关问答推荐

Laravel:启动下载并同时在Blade 中显示Flash消息

通过激活模板在数据库中创建表

Mysqli_stmt::Execute():结果字段元数据中存在过早的EOF

如何减少Laravel分页中的链接数

添加登录:需要app.yaml仍然允许所有拥有Google帐户的人访问

我的邮箱中的链接在我的URL中添加了一个

在WooCommerce中只允许高价产品的BACS付款

PHP SimpleXML:按属性值访问 node

如何批量处理消息总线中的消息

Woocommerce API - 图像问题

PHP访问子数组列表出错结果和长度

使用 Spomky Labs JWT 框架中的 verifyWithKey 验证 id_token JWT 的签名需要 30 秒以上

使用 foreach php 将记录正确分配给正确的父级

在 WordPress 中将自定义帖子类型永久链接设置为 root

正则表达式将文本替换为标签 html 以字符开头

PHP 创建 CSV 文件并用波斯语写入

docker |管道失败的 ubuntu 源列表

将变量从中间件传递到控制器 Laravel

Symfony 6 中的入口点 (public/index.php) - 它是如何工作的?

如何在 Laravel 9 中判断异常是否可报告?