I have the following script which creates a multidimensional array from a MySQL query and prints the results according to categories like:
CSF (infection)
Glucose
Protein (Spot Urine)
Blood gases
Bicarbonate (Fluid)
pH (Fluid)
Oxygen partial pressure (tension)(pO2)
Creatinine clearance
Creatinine (Fluid)
Creatinine (24 hour Urine)
$test_groups = array();
$query = "SELECT * FROM
lab_test,
model_lab_test_lookup,
lab_test_group
WHERE
lab_test.lab_test_pk = model_lab_test_lookup.lab_test_fk
AND
model_lab_test_lookup.lab_test_group_fk = lab_test_group.lab_test_group_pk
AND
model_lab_test_lookup.pathway_fk = '$pathway'
GROUP BY lab_test.lab_test_pk";
$result = mysql_query($query, $connection) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
$test_groups[$row['group_name']][] = $row['lab_test'];
}
foreach($test_groups as $group_name => $tests){
echo '<strong>' . $group_name . '</strong><br />';
foreach($tests as $test){
echo $test . '<br />';
}
echo '<p>';
}
Now I want to add columns in addition to $row['lab_test']
(the test name), eg $row['lab_test_pk']
and $row['interval']
and be able to access those columns as happens in the foreach
loops.
Like
$test_groups[$row['group_name']] = array(
'test_pk' => $row['lab_test_pk'],
'test_name' => $row['lab_test'],
'interval' => $row['interval']
);
What is the right way of doing this and then accessing the added columns in the last foreach loop?
$row['lab_test_pk'].','.$row['lab_test'].','.$row['interval'];
Try the above line and use . to append.
Using part solution provided by @NMN I have the following working:
while ($row = mysql_fetch_assoc($result)){
$test_groups[$row['group_name']][] = $row['lab_test_pk'].','.$row['lab_test'].','.$row['interval'];
}
foreach($test_groups as $group_name => $tests){
echo '<strong>' . $group_name . '</strong><br />';
foreach($tests as $test){
list($test_pk, $test_name, $test_interval) = explode(',', $test);
echo $test_pk . '<br />';
echo $test_name . '<br />';
echo $test_interval . '<br />';
}
echo '<p>';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.