Page 1 of 1

extracting a multi-dimesional array

Posted: Sun Mar 02, 2003 2:30 am
by moniarde
Let me start with the code:

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
}
?>
Now to explain.

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.

Posted: Sun Mar 02, 2003 7:54 am
by volka
$totalarr[$value] = mysql_fetch_assoc($result);
FALSE will be assigned to $totalarr[$value] if there is no such record to fetch. You can test this e.g. by

Code: Select all

if ($value !== FALSE)

Code: Select all

<?php
...
foreach($totalarr as $key => $value)
{
	// $value "is" the array from mysql_fetch_assoc($result)
	// first let's output the values only (html output)
	echo $key, '=';
	if ($value !== FALSE)
	{
		//   extract($totalarr[$key]);   // only if necessary
		echo join (',', $value), '<br />';
	}
	else
		echo '<empty>< br/>'; // edit: missing '
}
  
// but maybe you want the column-name, too
foreach($totalarr as $key => $value)
{
	echo '<fieldset><legend>',$key , '</legend>';
	if ($value !== FALSE)
	{
		// extract($totalarr[$key]); // only if necessary
		foreach($value as $colname=>$colval)
			echo $colname, '=', $colval, '<br />';
	}
	else
		echo '<empty>';
	echo '</fieldset>';
}   	
?>
(script tested not even by compiler and I'm a bit groggy today ;) )

Posted: Mon Mar 03, 2003 4:05 am
by moniarde
Cool, thankyou very much volka. It all works perfectly.