【问题标题】:How to compare two array value and get matched inner array value in PHP如何在 PHP 中比较两个数组值并获得匹配的内部数组值
【发布时间】:2021-11-17 08:51:56
【问题描述】:

如何比较两个数组值并从内部数组中删除重复值并使用 PHP 存储在另一个数组中。由array[name] 比较的数组,并希望删除所有内部数组中的相关索引。

数组 1

Array
(
    [0] => detail12.docx
    [1] => resume.docx
)

数组 2

Array
(
    [name] => Array
        (
            [0] => detail12.docx
            [1] => document.pdf
            [2] => resume.docx
        )

    [type] => Array
        (
            [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            [1] => application/pdf
            [2] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
        )

    [tmp_name] => Array
        (
            [0] => /tmp/php2LK7xC
            [1] => /tmp/phpTEWqXG
            [2] => /tmp/phpAKki0M
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
        )

    [size] => Array
        (
            [0] => 30887
            [1] => 86118
            [2] => 30887
        )

)

预期输出:-

Array
(
    [name] => Array
        (
            [0] => detail12.docx
            [1] => resume.docx
        )

    [type] => Array
        (
            [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            [1] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
        )

    [tmp_name] => Array
        (
            [0] => /tmp/php2LK7xC
            [1] => /tmp/phpAKki0M
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 30887
            [1] => 30887
        )
)

我需要上述预期输出中所示的输出。请提供代码以获得解决方案!

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    此方法将name中未找到的键保存为array_search,稍后使用foreach删除。

    $a = ['detail12.docx', 'resume.docx'];
    $b = [
        'name' => ['detail12.docx', 'document.pdf', 'resume.docx', 'test.php'],
        'type' => ['application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'test']
    ];
    $keys = [];
    foreach($b['name'] as $index => $value){
        if(array_search($value, $a) === false){
            $keys[] = $index;
        }
    }
    foreach($b as $index => $array){
        foreach($keys as $key){
            unset($b[$index][$key]);
        }
        $b[$index] = array_values($b[$index]);
    }
    print_r($b);
    

    输出:

    Array
    (
        [name] => Array
            (
                [0] => detail12.docx
                [1] => resume.docx
            )
    
        [type] => Array
            (
                [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
                [1] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            )
    
    )
    

    Fiddle of code - Image of result
    附言。当然,我创建了一个包含一些条目的示例,只是作为视觉测试

    参考:

    【讨论】:

    • 它正在工作,但它可以按顺序获取数组索引 - [0] [1] 现在它像 - [0] [2]
    • 是的,我添加代码,你需要添加array_values
    • 不,不是,example,你又复制了我的代码吗?我加了一行。
    • 您可以看到数组 [0] 和 [2] 即将到来。因此它显示错误(消息:array_values() 只需要 1 个参数,给定 2 个参数)。请告诉我如何解决它。
    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多