suchen

Heim  >  Fragen und Antworten  >  Hauptteil

PHP比较二维数组,求大神赐教

一个参与活动名单的二维数组
$arr1=array(
array('name'=>'张三','phone'=>'1354459845','address'=>'一街5号'),
array('name'=>'李四','phone'=>'1323439845','address'=>'一街3号'),
array('name'=>'陈五','phone'=>'1354353145','address'=>'一街2号'),
);

一个历史中奖名单的二维数组
$arr2=array(
array('name'=>'mary','phone'=>'1313598445','address'=>'一街8号'),
array('name'=>'jhon','phone'=>'1323298425','address'=>'一街9号'),
array('name'=>'陈五','phone'=>'1353dsad45','address'=>'一街10号'),
);
现在要比较这两个数组,只要名称name或phone或address其中一个在历史名单中出现过,就要把他从arr1参与活动名单中去掉。
除了两次foreach再比较以外,有什么好的方法可以快速地得出筛选后的活动名单结果。两份名单都至少几千,如果用两次foreach的话速度会很慢

巴扎黑巴扎黑2753 Tage vor402

Antworte allen(2)Ich werde antworten

  • 漂亮男人

    漂亮男人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);
    

    Antwort
    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
    

    Antwort
    0
  • StornierenAntwort