开发者

Foreach loop prints to many values

update, solved

I have the following 2 array's (with the same length), I want to display these values in a table. The problem is he prints just my first array ($gPositionStudents) and not the values of array $gPositionInternships. The goal is to link each student to a different internship, but I know there is no relationship between these two array's. I'm looking for a function that can shift these 2 array's so that each time I shift the student has a other internship.

Importend to know, I did a small modification on array $gStartPositionInternship, I made this array equal in size as the length of array $gStartPositionStudents. With following code: enter code here// Make items in $gStartPositionInternships as long as array $gStartPositionStudents $gStartPositionInternships = array_pad($internships, $lengthStudentArray, 'null');

I included my current output, see image:

Foreach loop prints to many values

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;

var_dump of $gPositionStudents

 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 of: $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)"


$rows[] assignment is inside the nested loop - it looks suspicious to me. Try to put that assignment after the nested loop, like this:

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);
}


Are your original arrays populated as expected ? Try print_f to figure it out. As far as I see if your original arrays have both 2 entries you should get a 4 entries array with like :

$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
            )
        )
    )
)

is that really what you expect ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜