search

Home  >  Q&A  >  body text

PHP compares two-dimensional arrays, please give me some advice

A two-dimensional array of the list of participants
$arr1=array(
array('name'=>'Zhang San','phone'=>'1354459845','address'=> ;'No. 5, First Street'),
array('name'=>'李思','phone'=>'1323439845','address'=>'No. 3, First Street'),< br>array('name'=>'Chen Wu', 'phone'=>'1354353145', 'address'=>'No. 2, First Street'),
);

A two-dimensional array of historical lottery winning lists
$arr2=array(
array('name'=>'mary', 'phone'=>'1313598445', 'address'=> 'No. 8, First Street'),
array('name'=>'jhon', 'phone'=>'1323298425', 'address'=>'No. 9, First Street'),
array('name'=>'Chen Wu', 'phone'=>'1353dsad45', 'address'=>'No. 10, First Street'),
);
Now we need to compare these two An array, as long as one of the names name, phone or address appears in the historical list, he will be removed from the arr1 participating activity list.
In addition to two foreach comparisons, is there any good way to quickly get the filtered activity list results? Both lists are at least several thousand. If you use foreach twice, the speed will be very slow

巴扎黑巴扎黑2753 days ago403

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-05-16 13:10:04

    $arr = [
        ["name" => "李四", "phone" => '5555', "address" => 'aaa' ],
        ["name" => "张三", "phone" => '6666', "address" => 'bbb' ],
        ["name" => "老王", "phone" => '7777', "address" => 'ccc' ],
    ];
    $filter = [
        ["name" => "李四", "phone" => '3333', "address" => 'xyz' ],
        ["name" => "王三", "phone" => '6666', "address" => 'xyz' ],
    ];
    
    $arr = array_filter($arr, function($i) use ($filter) {
        return !in_array($i['name'], array_column($filter, 'name')) && !in_array($i['phone'], array_column($filter, 'phone'));
    });
    var_dump($arr);
    

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:10:04

    $arr1 = [];
    $arr2 = [];
    
    ///////准备测试数据
    for ($i = 0; $i < 100000; ++$i) {
        $arr1_sub = [];
        $arr1_sub ['name'] = sprintf('测试%s', rand(0, 10000));
        $arr1_sub ['phone'] = sprintf('13%s', rand(111111111, 999999999));
        $arr1_sub ['address'] = sprintf('一街%s号', rand(0, 10000));
        $arr1[] = $arr1_sub;
    
        $arr2_sub = [];
        $arr2_sub['name'] = sprintf('测试%s', rand(0, 10000));
        $arr2_sub['phone'] = sprintf('13%s', rand(111111111, 999999999));
        $arr2_sub['address'] = sprintf('一街%s号', rand(0, 10000));
        $arr2[] = $arr2_sub;
    }
    
    //////测试开始
    $begin = microtime(true);
    
    $diff_key_array = [
        'name' => 'phone',
        'phone' => 'phone',
        'address' => 'phone',
    ];
    $diff_res_array = [];
    
    ///1、根据 key,缓存对一个数据差
    foreach ($diff_key_array as $k => $v) {
        ///找到对应的 name、address、phone 需要留下的数据
        $diff_res_array[$k] = array_diff_assoc(array_column($arr2, $v, $k), array_column($arr1, $v, $k));
    }
    unset($arr1); //清理不需要的数组 arr2
    
    ///过滤对应 arr2 数据
    $result = array_filter($arr2, function ($value) use ($diff_key_array, &$diff_res_array) {
        foreach ($diff_key_array as $k => $v) {
            $item = $value[$k];
            //利用 php 中,hash 找 key 的方案,实现解决 O(1)查找
            if (!isset($diff_res_array[$k][$item])) {
                return false;
            }
        }
        return true;
    });
    unset($diff_key_array);
    unset($diff_res_array);
    
    $exec_time = microtime(true) - $begin;
    var_dump($exec_time);
    var_dump($result);
    
    /// 10万数据,用时约 float  0.69 s
    ///100万数据,用时约 float 14.86 s
    

    reply
    0
  • Cancelreply