I have created a 3 dimensional array (Fig 1) from a MySQL query (fig 2). The top level is [Expense_ID] and the next level up is [Participant_ID]. There can be multiple Participant_ID's for each Expense_ID.
Qu 1) Although the data is all repeated at the bottom level of the array, please could someone tell me how I can iterate through this array as it is (at each level) to echo out the structure as per the original data (if I can echo it out, then I can perform the calculations I need to do). ie in the echo statement, [Expense_ID] would be taken from the top level of the array, [Participant_ID] would be taken from level 2 and the remaining 3 fields would be taken from the bottom level.
Qu 2) I don't know if this is the best array structure. The code I used to get to this array is shown in fig 3. How could I change this so that level one shows as [0] => 86 and level 2 is [0] => 130, 1 => 135 This might be easier to work with.
Qu 2a) How then would I iterate through the array if it is in this format?
Any help would be much appreciated.
Array
(
[86] => Array
(
[130] => Array
(
[Expense_ID] => 86
[Expense_Description] => Item1
[Total_Amount] => 3000.00
[Expense_Payer_ID] => 134
[Participant_ID] => 130
)
[135] => Array
(
[Expense_ID] => 86
[Expense_Description] => Item1
etc.
$result = array();
while ($row = mysql_fetch_array($Exps)){
$Exp_ID = $row['Expense_ID'];
$Participant_ID = $row['Participant_ID'];
$result[$Exp_ID][$Participant_ID] = array(
'User_ID' => $row['User_ID'],
'Expense_ID' => $row['Expense_ID'],
etc.
);
}
I'm not sure I understand what you are asking but I think a simple foreach loop will do the trick.
foreach ($result as $id => $participants) {
// here your Expense_ID == $id
foreach ($participants as $pid => $value) {
// here Participant_ID == $pid
//
// Total_Amount == $value['Total_Amount']
// Expense_Description == $value['Expense_Description']
// ....
}
}
Hope this can help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.