note 74243 added to language.types.array
| From: | rga at merchantpal dot com | Date: | Sat, 31 Mar 2007 18:36:23 +0000 |
| Subject: | note 74243 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124977@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 216.194.113.175
Probable Submitter: 70.112.103.231
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/74243
Del: integrated -- https://master.php.net/note/delete/74243/integrated
Del: useless -- https://master.php.net/note/delete/74243/useless
Del: bad code -- https://master.php.net/note/delete/74243/bad+code
Del: spam -- https://master.php.net/note/delete/74243/spam
Del: non-english -- https://master.php.net/note/delete/74243/non-english
Del: in docs -- https://master.php.net/note/delete/74243/in+docs
Del: other reasons-- https://master.php.net/note/delete/74243
Reject -- https://master.php.net/note/reject/74243
Search -- https://master.php.net/manage/user-notes.php