note 48270 added to function.array
| From: | stephen | Date: | Fri, 17 Dec 2004 12:57:21 +0000 |
| Subject: | note 48270 added to function.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81907@lists.php.net to get a copy of this message | ||
I think I may have found a simpler, slightly more logical route to solving the previous problem. I
came across this solution when retreving data from MySql which in most cases was for individual bits
of data, but also needed to reference a set of sub data from a second table.
<?php
// -------------------------------------------------------
// Function: db_get_data()
// Desc: Build array of data from mysql table and sub table(s)
// PreCondition: None
// Returns: Array of rows / sub rows from db,
// -------------------------------------------------------
function db_get_data()
{
$ar_sub = array();
/* [ .... code for connecting and querying DB .... ] */
// Retireve from first table
$row = mysql_fetch_row($result);
// Get first two fields
$ar_details['item0'] = $row ['field0'];
$ar_details['item1'] = $row ['field1'];
// Using result from query on sub table
// for each sub row, 'push' data on sub array
while($row_sub = mysql_fetch_array($result_sub))
{
array_push($ar_sub, $row_sub['sub_field']);
}
// Add sub array as element of parent array
$ar_details['item3'] = $ar_sub;
return $ar_details;
}
// -------------------------------------------------------
// Retrieving array data
// -------------------------------------------------------
// Call function
$arr_details = db_get_data()
// Output Data
echo $ar_details['item1'];
echo $ar_details['item2'];
// For each record in sub array....
for($i=0; $i<count($ar_details['item3']); $i++)
{
echo $ar_details['item3'][$i];
}
?>
For extra sub tables the process can be repeated by adding more sub arrays, and each sub array can
hold as much data as you like. Therefore allowing you to build an ever expanding tree as required.
If you have the option I would recommend using nested classes though :)
----
Manual Page -- http://www.php.net/manual/en/function.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48270
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48270&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48270&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48270&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48270&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48270&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48270&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48270&report=yes
Search -- http://master.php.net/manage/user-notes.php