Code: Select all
<?php
$testarr[] = "appdet";
$testarr[] = "chldn";
$testarr[] = "cp1";
$testarr[] = "cp2";
$testarr[] = "dent";
$testarr[] = "doc";
$testarr[] = "fam";
$testarr[] = "fo";
$testarr[] = "misc";
$testarr[] = "mo";
$testarr[] = "stud";
foreach($testarr as $key => $value) {
$sql = "Select * from ".$value.
" Where appdet_id = '".$id."'";
$result = mysql_query($sql,$db)
or die(mysql_error());
$totalarr[$value] = mysql_fetch_assoc($result);
}
foreach($totalarr as $key => $value) {
if ($value == "") {
$value = ""empty"";
}
if (is_array($key)) {
extract($key);
}
if (!$value == "empty") {
foreach($totalarr[$key] as $key => $value) {
if ($value == "") {
$value = ""empty"";
}
echo "$key = $value<br>";
}//end foreach totalarr[$key]
}//end if
}
?>I am trying to extract all the keys and all the variables from a 2 level array; that is, the first level of $totalarr contains variables which are arrays, and I want to extract all the keys from the 2nd level into variables. It can't be done just by extract - if so, I would not be here. I need to extract the 2nd lvl arrays dynamically, so without actually referring to the names of the arrays. What I have been trying to do is to use foreach to gain access to the 1st lvl, and then, by referring to the keys as $key, trying to extract $totalarr[$key], and this is where it seems to be falling over.
The names of the 1st level of $totalarr and created from the names of the tables I am getting the info from, which means that I can refer to the 1st lvl arrays by the same names as are assigned to $testarr, if necessary.
I am also trying to avoid error messages brought up by the foreach statement when the 1st lvl of totalarr contains an empty array within it because a table is empty (which is valid). The line "if(!$value == "empty")" is an attempt at containing that, but it also kills any output from the other arrays for some reason, and I can't see why.
Can anyone give me any clues on how to fully and dynamically extract to variable a multi-dimensional array?
Also, is it possible to generate variable names by using the value of another variable?
PS I do have a way of getting the tablenames dynamically already, I was just using $testarr as part of the testing phase.