我怎么能用PHP创建这个数组?

时间:2016-06-10 19:15:14

标签: php arrays foreach

我有一个来自数据库的查询,它返回一个值数组并放入$history array。 每个值都包含course_idunit_idtime_stamp

我想要另一个数组包含course_id和相应的unit_ids

course_id必须拥有最新的time_stamp,并且每个unit_id都必须拥有time_stamp

每个课程array还需要最多4个单元。

(幸运的是,来自DB的$history数组已经是ORDER BY time_stamp DESC所以我想我只需要获得课程的第一个time_stamp和每个课程的前4个单元。 / p>

所以我需要的是:

courseListArray:
  array[0]:
  course_id: closest time_stamp
    unit_id: time_stamp
    unit_id: time_stamp
    unit_id: time_stamp
    unit_id: time_stamp
  array[1]:
  course_id: closest time_stamp
    unit_id: time_stamp
    unit_id: time_stamp
    unit_id: time_stamp
    unit_id: time_stamp

关注数据库查询的示例:

array(6) { ["course_id"]=> string(4) "2968" [0]=> string(4) "2968" ["unit_id"]=> string(4) "3002" [1]=> string(4) "3002" ["time_stamp"]=> string(26) "2016-06-10 14:54:11.000000" [2]=> string(26) "2016-06-10 14:54:11.000000" } 

array(6) { ["course_id"]=> string(4) "2968" [0]=> string(4) "2968" ["unit_id"]=> string(4) "3006" [1]=> string(4) "3006" ["time_stamp"]=> string(26) "2016-06-10 14:41:54.000000" [2]=> string(26) "2016-06-10 14:41:54.000000" } 

array(6) { ["course_id"]=> string(4) "2740" [0]=> string(4) "2740" ["unit_id"]=> string(4) "2982" [1]=> string(4) "2982" ["time_stamp"]=> string(26) "2016-06-10 13:20:01.000000" [2]=> string(26) "2016-06-10 13:20:01.000000" } 

array(6) { ["course_id"]=> string(4) "3017" [0]=> string(4) "3017" ["unit_id"]=> string(4) "3012" [1]=> string(4) "3012" ["time_stamp"]=> string(26) "2016-06-10 13:12:19.000000" [2]=> string(26) "2016-06-10 13:12:19.000000" } 

我已尝试过以下代码,但它没有按预期工作,因为array keys填写不正确。

foreach ($history as $data) {
    if (!array_key_exists($data['course_id'], $courseList)) {
        array_push($courseList, $data['course_id']);
        $courseList[$data['course_id']] = array();
    }

    if (count($courseList[$data['course_id']]) <= 4) {
        array_push($courseList[$data['course_id']], $data['unit_id']);
    }
}

2 个答案:

答案 0 :(得分:1)

您正在混合索引和关联数组。您应该使$course_array成为关键数组,其关键字为course_id。该值应该是一个子数组,其中包含该课程ID的所有行。

foreach ($history as $data) {
    $cid = $data['course_id'];
    if (!isset($courseArray[$cid])) {
        $courseArray[$cid] = array();
    }
    $courseArray[$cid][$data['unit_id']] = $data['time_stamp'];
}

答案 1 :(得分:0)

知道了:

foreach ($history as $data) {
    if (!array_key_exists('course'.$data['course_id'], $courseList)) {
        array_push($courseList, $data['course_id']);

        $courseList['course'.$data['course_id']] = array();
        $courseList['course'.$data['course_id']]['units'] = array();
        $courseList['course'.$data['course_id']]['course'] = [$data['course_id'] => $data['time_stamp']];
    }

    if (count($courseList['course'.$data['course_id']]['units']) <= 4) {
        array_push($courseList['course'.$data['course_id']]['units'], [$data['unit_id'] => $data['time_stamp']]);
    }
}

foreach ($courseList as $key => $course) {
    if (!is_numeric($key)) {
        var_dump($course);
        echo '<BR><BR>';
    }
}

结果:

array(2) { ["units"]=> array(2) { [0]=> array(1) { [3002]=> string(26) "2016-06-10 14:54:11.000000" } [1]=> array(1) { [3006]=> string(26) "2016-06-10 14:41:54.000000" } } ["course"]=> array(1) { [2968]=> string(26) "2016-06-10 14:54:11.000000" } } 

array(2) { ["units"]=> array(1) { [0]=> array(1) { [2982]=> string(26) "2016-06-10 13:20:01.000000" } } ["course"]=> array(1) { [2740]=> string(26) "2016-06-10 13:20:01.000000" } } 

array(2) { ["units"]=> array(1) { [0]=> array(1) { [3012]=> string(26) "2016-06-10 13:12:19.000000" } } ["course"]=> array(1) { [3017]=> string(26) "2016-06-10 13:12:19.000000" } } 

编辑:

Barmar的答案更好。