顶级键上的PHP合并数组

I have 1 arrays:

array(5) { // This is the keys on the CSV.
  [0]=>
  array(4) {
    [0]=>
    string(4) "user"
    [1]=>
    string(4) "date"
    [2]=>
    string(3) "md5"
    [3]=>
    string(4) "sha1"
  }
  [1]=>
  array(4) {
    [0]=>
    string(4) "user1"
    [1]=>
    string(8) "02/02/15"
    [2]=>
    string(6) "123456"
    [3]=>
    string(5) "54321"
  }
  [2]=>
  array(4) {
    [0]=>
    string(4) "user1"
    [1]=>
    string(8) "02/03/15"
    [2]=>
    string(6) "123456"
    [3]=>
    string(5) "54321"
  }
  [3]=>
  array(4) {
    [0]=>
    string(5) "user2"
    [1]=>
    string(8) "02/02/15"
    [2]=>
    string(6) "112233"
    [3]=>
    string(6) "332211"
  }
  [4]=>
  array(4) {
    [0]=>
    string(5) "user2"
    [1]=>
    string(8) "02/03/15"
    [2]=>
    string(6) "112244"
    [3]=>
    string(6) "332244"
  }
}

So User 1 and User 2 each have a file that is MD5/SHA1 sum'd and checked.

I would like the data to turn from the above, to this:

array(3) {
  ["user"]=>
  array(1) {
    ["date"]=>
    array(2) {
      [0]=>
      string(3) "md5"
      [1]=>
      string(4) "sha1"
    }
  }
  ["user1"]=>
  array(1) {
    ["02/02/15"]=>
    array(2) {
      [0]=>
      string(6) "123456"
      [1]=>
      string(5) "54321"
    }
    ["02/03/15"]=>
    array(2) {
      [0]=>
      string(6) "123456"
      [1]=>
      string(5) "54321"
    }
  }
  ["user2"]=>
  array(1) {
    ["02/02/15"]=>
    array(2) {
      [0]=>
      string(6) "112233"
      [1]=>
      string(6) "332211"
    }
    ["02/03/15"]=>
    array(2) {
      [0]=>
      string(6) "112244"
      [1]=>
      string(6) "332244"
    }
  }
}

So far, I'm close. Here is my code:

<?php
error_reporting(E_ALL & ~E_NOTICE);

//Reading csv

$csv = array_map('str_getcsv',file('/var/www/sums.csv'));

debug($csv);
//User list
$userData = array();

foreach ($csv as $user) {
        $ts = $user[1];
        $un = $user[0];
        $sums = array($user[2],$user[3]);
        $userPreProc[$un] = array($ts => $sums);
        $userData = array_merge($userPreProc, $userData);
}

debug($userData);

function debug($db) {
        echo "<pre>";
        var_dump($db);
        echo "</pre>";
}
?>

The issue here is that only the first 'user1' and 'user2' entries are merged to the new array, so it looks like this:

array(3) {
  ["user"]=>
  array(1) {
    ["date"]=>
    array(2) {
      [0]=>
      string(3) "md5"
      [1]=>
      string(4) "sha1"
    }
  }
  ["user1"]=>
  array(1) {
    ["02/02/15"]=>
    array(2) {
      [0]=>
      string(6) "123456"
      [1]=>
      string(5) "54321"
    }
  }
  ["user2"]=>
  array(1) {
    ["02/02/15"]=>
    array(2) {
      [0]=>
      string(6) "112233"
      [1]=>
      string(6) "332211"
    }
  }
}

Any suggestions how I can ensure that all the keys are merged as expected?

Just change your foreach loop to be like this. You don't need to merge your array via function.

foreach ($csv as $user) {
        $ts = $user[1];
        $un = $user[0];
        $sums = array($user[2],$user[3]);
        $userData[$un][$ts] = $sums;
}