By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,408 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,408 IT Pros & Developers. It's quick & easy.

multidimensional associatve array problem

P: n/a
from a MySQL DB i want to get a multidimensional array that i can loop
through

either key =field name value = array of ENUM options
or array[x][0] =field name, array[x][1]= ENUM options and increment x
inside loop
$q=mysql_query("SHOW FIELDS FROM table" ) or die ("Query failed");

while ($row = mysql_fetch_array($q))
{
//from http://uk.php.net/mysql_fetch_field

echo 'field is '.$row['Field'] . ' type is ' . $row['Type'];
if (ereg('enum.(.*).', $row['Field'], $match))
{
$opts = explode(',', $match[1]);
foreach ($opts as $item)
{$finalResult[] = substr($item, 1, strlen($item)-2);}
}

$array=array ($row['Field'] =>$finalResult[]);

}
gives Fatal error :Cannot use [] for reading in .......

a similar error message when i try a multidimentional array
array[$x][0]=$row['Field']
array[$x][1]=$finalResult[]
anyone know the answer ( or a better way of doing it )

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
pauld (pd****@yahoo.co.uk) decided we needed to hear...
from a MySQL DB i want to get a multidimensional array that i can loop
through

either key =field name value = array of ENUM options
or array[x][0] =field name, array[x][1]= ENUM options and increment x
inside loop
<snip failing code>

Try the following instead (altered from your code)...

$q = mysql_query("show fields from thetable");
$arr = array();
while ($row = mysql_fetch_array($q)) {
echo 'field is ' . $row[0] . ' type is ' . $row[1] . '<br>';
$finalResult = array();
if (preg_match('/enum\((.*?)\)/', $row[1], $match)) {
$opts = explode(',', $match[1]);
foreach ($opts as $item) {
array_push($finalResult, substr($item, 1, strlen($item)-2));
}
}
$arr[$row[0]] = $finalResult;
}
print_r($arr);

--
Dave <da**@REMOVEbundook.com>
(Remove REMOVE for email address)
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.