Foreach循环打印到许多值

时间:2011-04-01 07:10:01

标签: php drupal drupal-6

更新,已解决

我有以下2个数组(长度相同),我想在表格中显示这些值。问题是他打印的只是我的第一个数组($ gPositionStudents)而不是数组$ gPositionInternships的值。 目标是将每个学生连接到不同的实习,但我知道这两个数组之间没有关系。我正在寻找一个可以改变这两个阵列的功能,这样每次换班时学生都可以进行另一次实习。

Importend要知道,我对数组$ gStartPositionInternship做了一个小修改,我把这个数组的大小调整为数组$ gStartPositionStudents的长度。使用以下代码: enter code here //只要数组$ gStartPositionStudents在$ gStartPositionInternships中创建项目  $ gStartPositionInternships = array_pad($ internships,$ lengthStudentArray,'null');

我包含了我当前的输出,请参阅图片: enter image description here

enter code here// Make table
$header = array();
$header[] = array('data' => 'Internship');
$header[] = array('data' => 'UGentID');
// this big array will contains all rows
// global variables.
global $gStartPositionStudents;
global $gStartPositionInternships;
$rows = array();
foreach($gStartPositionStudents as $value) {
    foreach($gStartPositionInternships as $key=>$value2) {
        // each loop will add a row here.
        $row = array();
        // build the row
        $row[] = array('data' => $value[0]['value']);
        $row[] = array('data' => $value2);
    }
    // add the row to the "big row data (contains all rows)
    $rows[] = array('data' => $row);
}
$output = theme('table', $header, $rows);
return $output;

$ gPositionStudents的var_dump

 array(148) {
  [0]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "804868"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "804869"
    }
  }
  [2]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "705169"
    }
  }
  [3]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "805148"
    }
  }
  [4]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "702342"
    }
  }
  [5]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "803176"
    }
  }
  [6]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "706651"
    }
  }
  [7]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "706333"
    }
  }
  [8]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "806026"
    }
  }
  [9]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(6) "702808"
    }
  }

var_dump:$ gPositionInternships

array(148) {
  [0]=>
  string(18) "Pcv (campus Aalst)"
  [1]=>
  string(53) "Mss ( Privaatpraktijk kinesitherapie Walravens Marc )"
  [2]=>
  string(54) "Mss ( Privaatpraktijk kinesitherapie Peeters Stefaan )"
  [3]=>
  string(35) "psychiatrie (campus Vercruysselaan)"
  [4]=>
  string(39) "interne geneeskunde (campus Loofstraat)"
  [5]=>
  string(40) "interne geneeskunde (campus Kennedylaan)"
  [6]=>
  string(29) "heelkunde (campus Loofstraat)"
  [7]=>
  string(30) "heelkunde (campus Kennedylaan)"
  [8]=>
  string(33) "heelkunde (campus Vercruysselaan)"
  [9]=>
  string(38) "logopedie (groepspraktijk Logomatopee)"
  [10]=>
  string(41) "logopedie (Koninklijk Instituut Spermali)"
  [11]=>
  string(34) "Fysieke activiteit (To Walk Again)"
  [12]=>
  string(53) "algemene en plastische heelkunde ( AZ AZ Oudenaarde )"
  [13]=>
  string(38) "dermatologie (campus Maria Middelares)"
  [14]=>
  string(29) "NKO (campus Maria Middelares)"
  [15]=>
  string(38) "dermatologie (campus Maria Middelares)"
  [16]=>
  string(38) "Fysieke activiteit (Beweegkamp Vlabus)"
  [17]=>
  string(43) "Hoofdverpleegkundige ( UZ UZ Gent Urologie)"
  [18]=>
  string(66) "Opleidingscoördinator ( Onderwijsinstelling Arteveldehogeschool )"
  [19]=>
  string(90) "Verpleegkundig Specialist ( UMC Universitair Medisch Centrum Universitair Medisch Centrum)"
  [20]=>
  string(31) "Mss ( AZ Nikolaas campus Hamme)"
  [21]=>
  string(74) "Mss ( Privaatpraktijk kinesitherapie Cuigniez Pascale PR Cuigniez Pascale)"

2 个答案:

答案 0 :(得分:1)

$ rows []赋值在嵌套循环中 - 它看起来很可疑。尝试在嵌套循环之后放置该赋值,如下所示:

foreach($studentUGentID as $key=>$value) {
    foreach($internshipNaam as $key2=>$value2) {
    ...
    }
    // add the row to the "big row data (contains all rows)
    $rows[] = array('data' => $row);
}

答案 1 :(得分:0)

原始阵列是否按预期填充?试试print_f搞清楚。据我所知,如果您的原始数组同时包含两个条目,您应该得到一个4条目数组,其中包含:

$rows = array(
    [0] => array(
        'data' => array(
            [0] => array(
                'data' => value
            ),
            [1] => array(
                'data' => value
            )
    ),
    [1] => array(
        'data' => array(
            [0] => array(
                'data' => value
            ),
            [1] => array(
                'data' => value
            )
    ),
    [2] => array(
        'data' => array(
            [0] => array(
                'data' => value
            ),
            [1] => array(
                'data' => value
            )
    ),
    [3] => array(
        'data' => array(
            [0] => array(
                'data' => value
            ),
            [1] => array(
                'data' => value
            )
        )
    )
)

真的是你期待的吗?