douyingyu5573 2015-02-03 20:13
浏览 53
已采纳

顶级键上的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?

  • 写回答

1条回答 默认 最新

  • duanfanta6741 2015-02-03 20:21
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算