dongtuo6562
2018-05-21 12:00 阅读 30
已采纳

如何比较拼接的多维数组(具有大量数据)?

I have a huge array $properties with about 500.000 items:

  array(470000) {
    ["12345"]=>
    array(5) {
      ["dateTime"]=>
      string(19) "2016-10-12 19:46:25"
      ["fileName"]=>
      string(46) "monkey.jpg"
      ["path"]=>
      string(149) "Volumes/animals/monkey.jpg"
      ["size"]=>
      string(7) "2650752"
    }
    ["678790"]=>
    array(5) {
      ["dateTime"]=>
      string(19) "2016-10-12 14:39:43"
      ["fileName"]=>
      string(45) "elephant.jpg"
      ["path"]=>
      string(171) "Volumes/animals/elephant.jpg"
      ["size"]=>
      string(7) "2306688"
    }

... and so on.

So to make performance better, I spliced it into parts:

$splice_size = 10000;
        $count_arr = (count($properties)/$splice_size)-1;


        For($i=0; $i<$count_arr; $i++){
            $res[] = array_splice($properties, 0,$splice_size); 
        }
        $res[] = array_splice($properties, 0,count($properties)); 

Now my array looks like this:

array(4) {
  [0]=>
  array(10000) {
    ["12345"]=>
    array(5) {
      ["dateTime"]=>
      string(19) "2016-10-12 19:46:25"
      ["fileName"]=>
      string(46) "monkey.jpg"
      ["path"]=>
      string(149) "Volumes/animals/monkey.jpg"
      ["size"]=>
      string(7) "2650752"
    }
    ["678790"]=>
    array(5) {
      ["dateTime"]=>
      string(19) "2016-10-12 14:39:43"
      ["fileName"]=>
      string(45) "elephant.jpg"
      ["path"]=>
      string(171) "Volumes/animals/elephant.jpg"
      ["size"]=>
      string(7) "2306688"
    }

   ... and so on.
   }
  [1]=>....
  and so on....
}

I want now compare two of these arrays:

  function array_diff_assoc_recursive($array1, $array2)
                {
                    foreach($array1 as $key => $value)
                    {
                        if(is_array($value))
                        {
                            if(!isset($array2[$key]))
                            {
                                $difference[$key] = $value;
                            }
                            elseif(!is_array($array2[$key]))
                            {
                                $difference[$key] = $value;
                            }
                            else
                            {
                                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                                if($new_diff != FALSE)
                                {
                                    $difference[$key] = $new_diff;
                                }
                            }
                        }
                        elseif(!isset($array2[$key]) || $array2[$key] != $value)
                        {
                            $difference[$key] = $value;
                        }
                    }
                    return !isset($difference) ? 0 : $difference;
                }


                echo "<pre>";
                print_r(array_diff_assoc_recursive($new, $res));
                echo "</pre>";

But the system crashes. Too much data. So my question is, their must be an advantage of splicing the array (like making chunks) that I still do not get, to get better performance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐