note 74243 deleted from language.types.array by philip

From: Date: Sun, 01 Apr 2007 09:40:54 +0000
Subject: note 74243 deleted from language.types.array by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-124993@lists.php.net to get a copy of this message
Note Submitter: rga at merchantpal dot com ---- Populate multi-dimensional array with variable variable to dynamically name KEYs. <?php $r=0; $sql = "SELECT Some Data ..;"; if ($recs = runsql("$sql")) { // runsql function is in a lib, just runs the query $r = mysql_num_rows($recs); } $myDATA = array(); if ($r) { mysql_data_seek($recs,0); while ($rec = mysql_fetch_assoc($recs)) { // make, assign dynamic Key name $key = "rec" . $rec['id']; $$key = array(); foreach ($rec as $k => $v ) { $myDATA[$key][$k] = $v; } } } $test = $myDATA['someRec']['someField']; // or just loop through all Records, Fields foreach ($myDATA as $recno => $records ) { foreach ($records as $k => $v ) { echo "<li>$k : $v"; } } ?> Note: If you don't need the KEY naming, then I think using a numeric index is simpler.

« previous php.notes (#124993) next »