Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在两个数组之间比较三个值?[快车道]_Php_Arrays - Fatal编程技术网

Php 如何在两个数组之间比较三个值?[快车道]

Php 如何在两个数组之间比较三个值?[快车道],php,arrays,Php,Arrays,这是第一个数组的完整列表(大约1000条记录) 这是另一个(200records) 这就是我所期望的结果 我已经在PHP7中使用了递归函数,但是加载它仍然需要很多时间。对这些数组搜索有什么想法吗? array (size=3) 'matrixUuid' => string '1' (length=17) 'coursesUuid' => string '2' (length=17) 'employeesUuid' => string '3' (lengt

这是第一个数组的完整列表(大约1000条记录)

这是另一个(200records)

这就是我所期望的结果

我已经在PHP7中使用了递归函数,但是加载它仍然需要很多时间。对这些数组搜索有什么想法吗?

array (size=3)
    'matrixUuid' => string '1' (length=17)
    'coursesUuid' => string '2' (length=17)
    'employeesUuid' => string '3' (length=17)

虚拟数据

不确定它是否正是您所需要的,但请尝试以下方法:

array(0 => array('matrixUuid' => '1', 'coursesUuid' => '2', 'employeesUuid' => '3'), 1 => array('matrixUuid' => '4', 'coursesUuid' => '5', 'employeesUuid' => '6'), 2 => array('matrixUuid' => '7', 'coursesUuid' => '8', 'employeesUuid' => '9'));

这基本上只是我的想法,我不确定它的工作速度有多快,但如果我们谈论的是长度不太长的阵列(不是数百万左右),我会考虑以下算法:

  • 选择最短的数组
  • 将其解析为以下结构:
  • 只需通过最长的阵列启动简单循环:

  • 两个数组中基本上都有相同的元素?生成散列并比较它们。@vivek_23是的,使用(MatrixUID、coursesUuid和employeesUuid)我已经在一个数组之前完成了分组search@BryantTang请使用
    var\u导出
    而不是
    var\u转储
    print\r
    当你想得到问题的快速答案时。@MorganFreeFarm哦,这只是一个简单易读的例子,而不是最后的结果。我需要在网页上显示一个数字,我试图在两个数组中生成10k+的值,立即生效。这非常快,可以完成数据比较,谢谢~
    array (size=3)
        'matrixUuid' => string '1' (length=17)
        'coursesUuid' => string '2' (length=17)
        'employeesUuid' => string '3' (length=17)
    
    <?php
    
    public static function findElement($count, $obj, $element, $target)
    {
        $obj = parseToArray($obj);
        $target = parseToArray($target);
        if ($count > 0) {
            foreach ($element as $key => $data) {
                if ($key == 0) {
                    $find = ($obj[$count - 1][$data] == $target[$data]);
                } else {
                    $find = $find && ($obj[$count - 1][$data] == $target[$data]);
                }
            }
            if ($find) {
                return 1;
            } else {
                return self::findElement($count - 1, $obj, $element, $target);
            }
        } else {
            return 0;
        }
    }
    
    array(0 => array('matrixUuid' => '1', 'coursesUuid' => '2', 'employeesUuid' => '3'), 1 => array('matrixUuid' => '4', 'coursesUuid' => '5', 'employeesUuid' => '6'), 2 => array('matrixUuid' => '7', 'coursesUuid' => '8', 'employeesUuid' => '9'));
    
    function compareArrays($arr1, $arr2)
    {
       return strcmp(serialize($arr1), serialize($arr2));
    }
    
    $intersect = array_uintersect($arr1, $arr2, 'compareArrays');
    $intersect = array_values($intersect);
    print_r($intersect);
    
    [
        'matrixUuid1' => [
            'coursesUuid1' => [
                'employeesUuid1' => 1,
                'employeesUuid2' => 2,
                ...
            ],
            'coursesUuid2' => [
                ...
            ],
            ...
        ],
        'matrixUuid2' => [
            ...
        ],
        'matrixUuid3' => [
            ...
        ],
        ...
    ]
    
    $result = [];
    foreach ($longest as $itm) {
        if (isset($prepared[$itm['matrixUuid']][$itm['coursesUuid']][$itm['employeesUuid']])) {
            $result[] = $itm;
        }
    }