php - Creating a multidimensional array using an array and loop through mysql table -
i have table following structure. there 9 students enrolled in 3 classes (classes 01, 16 , 52), 3 enrollments in each class.
studentcode | classcode ------------------------- d3kcivyp | 01 ddihbc | 01 2vx0kv | 01 9p1pxwcv | 52 rnmfh1we | 52 mstbev | 52 yp4j3o | 16 fwq5oo | 16 6sxw3u | 16
this array current classes:
$classes_array = array ( [0] => 01 [1] => 52 [2] => 16 )
using array go through mysql table , create multidimensional array classcodes used first level keys:
array ( [01] => array ( [0] => d3kcivyp [1] => ddihbc [2] => 2vx0kv ) [52] => array ( [0] => 9p1pxwcv [1] => rnmfh1we [2] => mstbev ) [16] => array ( [0] => yp4j3o [1] => fwq5oo [2] => 6sxw3u ) ) foreach ($classes_array $key => $value) { $sql = "select studentcode table classcode = '$classes_array[$key]'"; $result = mysql_query($sql) or die(); while($data = mysql_fetch_array($result)) { $studentcodes_array[] = $data['studentcode']; } foreach ($studentcodes_array $studentcodeskey => $studentcodesvalue) { $multid_array[$value][] = $studentcodesvalue; } }
using code above, unfortunately don't array get. instead:
array ( [01] => array ( [0] => d3kcivyp [1] => ddihbc [2] => 2vx0kv ) [52] => array ( [0] => d3kcivyp [1] => ddihbc [2] => 2vx0kv [3] => 9p1pxwcv [4] => rnmfh1we [5] => mstbev ) [16] => array ( [0] => d3kcivyp [1] => ddihbc [2] => 2vx0kv [3] => 9p1pxwcv [4] => rnmfh1we [5] => mstbev [6] => yp4j3o [7] => fwq5oo [8] => 6sxw3u ) )
what wrong code? can me this?
you never initialise $studentcodes_array
remembering values previous loops.
try adding $studentcodes_array = array();
before while
.
Comments
Post a Comment