简单的PHP数组循环导致'递归'

时间:2015-01-23 11:01:52

标签: php drupal

我有一个非常奇怪的问题,当我使用for循环访问数组时会导致同一个对象被索引:

for ($g = $start_index; ($g < ($start_index + $groups_per_page)) && ($g < count($groups)); $g++) {
    dpm($g);
    dpm($groups);
    $group = $groups[$g];

    print $group->name;
}

$ groups最初由以下内容组成:

Array, 2 elements
    0 (object) stdClass
        last_date (String, 10 characters ) 1421335514
        sum_max_scores (String, 4 characters ) 3056
        assessment_count (String, 2 characters ) 37
        sum_max_scores_games (String, 3 characters ) 129
        passed_lesson_count (Float) 15
        sum_total_duration (String, 7 characters ) 1204553
        sum_total_duration_games (String, 7 characters ) 1007752
        first_date (String, 10 characters ) 1414158834
        sum_first_scores (String, 4 characters ) 2879
        id (String, 1 characters ) 1
        name (String, 32 characters ) Default Survival Language Group
        type (String, 2 characters ) sl
        can_add_content (String, 1 characters ) 1
        created (String, 1 characters ) 0
        teacher_id (String, 1 characters ) 1
        enabled (String, 1 characters ) 1
        lessons_in_group (Array, 199 elements)
        avg_first_scores (Float) 78
        avg_max_scores (Float) 83
        progress (Float) 5
    1 (object) stdClass
        last_date (String, 10 characters ) 1421335352
        sum_max_scores (String, 4 characters ) 5002
        assessment_count (String, 2 characters ) 58
        sum_max_scores_games (NULL)
        passed_lesson_count (Float) 41
        sum_total_duration (String, 6 characters ) 649810
        sum_total_duration_games (NULL)
        first_date (String, 10 characters ) 1414159584
        sum_first_scores (String, 4 characters ) 5002
        id (String, 1 characters ) 3
        name (String, 21 characters ) Default Phonics Group
        type (String, 2 characters ) ph
        can_add_content (String, 1 characters ) 1
        created (String, 1 characters ) 0
        teacher_id (String, 1 characters ) 1
        enabled (String, 1 characters ) 1
        lessons_in_group (Array, 103 elements)
        avg_first_scores (Float) 86
        avg_max_scores (Float) 86
        progress (Float) 0

循环很简单,你可以看到,dpm(Drupal调试输出)的输出是:

0
(see above)
Default Survival Language Group

然后:

1
Array, 2 elements
    0 (object) stdClass
        last_date (String, 10 characters ) 1421335514
        sum_max_scores (String, 4 characters ) 3056
        assessment_count (String, 2 characters ) 37
        sum_max_scores_games (String, 3 characters ) 129
        passed_lesson_count (Float) 15
        sum_total_duration (String, 7 characters ) 1204553
        sum_total_duration_games (String, 7 characters ) 1007752
        first_date (String, 10 characters ) 1414158834
        sum_first_scores (String, 4 characters ) 2879
        id (String, 1 characters ) 1
        name (String, 32 characters ) Default Survival Language Group
        type (String, 2 characters ) sl
        can_add_content (String, 1 characters ) 1
        created (String, 1 characters ) 0
        teacher_id (String, 1 characters ) 1
        enabled (String, 1 characters ) 1
        lessons_in_group (Array, 199 elements)
        avg_first_scores (Float) 78
        avg_max_scores (Float) 83
        progress (Float) 5
    1 (object) stdClass
        Recursion
Default Survival Language Group

正如您所看到的,这通过打印循环的BOTH迭代的第一个组的名称来体现。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

上一个($ group in $ groups)出现了问题