andyd34 Posted May 24, 2016 Share Posted May 24, 2016 I am trying to fetch the results from an array into a spreadsheet like table. Here is where the array is built array_push($array, array('id' => $row->IdEmployee, 'name' => $row->employeeName, 'date' => date("jS M Y", strtotime($row->Year.'W'.str_pad($row->Week, 2, 0, STR_PAD_LEFT).' +6 days')), 'week' => $row->Week, 'currency' => $row->CurrencySymbol, 'wage' => $row->Wage)); i want to fetch it like this <table> <tr> <th>Employee</th> <th>Week 1</th> <th>Week 2</th> <th>Week 3</th> <th>Week 4</th> // and so on, weeks will be dynamic </tr> foreach($x as $r) { <tr> <td>$r->name</td> <td>$r->val_wk_1</td> <td>$r->val_wk_2</td> <td>$r->val_wk_3</td> <td>$r->val_wk_4</td> </table> } I can get the table headers, names and week 1 but cant get my head around the values for the additional weeks Quote Link to comment https://forums.phpfreaks.com/topic/301250-foreach-loop/ Share on other sites More sharing options...
ginerjm Posted May 24, 2016 Share Posted May 24, 2016 I dont' see how example 1 relates to example 2 Quote Link to comment https://forums.phpfreaks.com/topic/301250-foreach-loop/#findComment-1533204 Share on other sites More sharing options...
mac_gyver Posted May 24, 2016 Share Posted May 24, 2016 (edited) when you have multiple sub-pieces of data for a particular main item, you would store the records as a sub-array using the main item value as the main array index. $array[$row->IdEmployee][] = array('id' => $row->IdEmployee, 'name' => $row->employeeName, 'date' => date("jS M Y", strtotime($row->Year.'W'.str_pad($row->Week, 2, 0, STR_PAD_LEFT).' +6 days')), 'week' => $row->Week, 'currency' => $row->CurrencySymbol, 'wage' => $row->Wage); you would loop over this using - foreach($array as $id=>$sub_array) { // $sub_array will be an array of the rows of data // start a new row in the output and output the name once here... // you can get a copy of the first row of data using $sub_array[0] // loop over the sub_array foreach($sub_array as $row) { // output the week information from each row } } Edited May 24, 2016 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/301250-foreach-loop/#findComment-1533208 Share on other sites More sharing options...
andyd34 Posted May 24, 2016 Author Share Posted May 24, 2016 I think i've been a little vague and my example is a little misleading. I want array_push($array, array('id' => $row->IdEmployee, 'name' => $row->employeeName, 'date' => date("jS M Y", strtotime($row->Year.'W'.str_pad($row->Week, 2, 0, STR_PAD_LEFT).' +6 days')), 'week' => $row->Week, 'currency' => $row->CurrencySymbol, 'wage' => $row->Wage)); to output like http://prntscr.com/b7umsp Quote Link to comment https://forums.phpfreaks.com/topic/301250-foreach-loop/#findComment-1533209 Share on other sites More sharing options...
Psycho Posted May 24, 2016 Share Posted May 24, 2016 I think i've been a little vague and my example is a little misleading. mac_gyver's response was correct, but you may have missed the point when trying to understand it with complex data. Let me give an example Instead of an array like this array( 'AAA' => ( 'week' => 'week1', 'value' => 'valueA' ) 'AAA' => ( 'week' => 'week2', 'value' => 'valueB' ) 'BBB' => ( 'week' => 'week1', 'value' => 'valueC' ) 'BBB' => ( 'week' => 'week2', 'value' => 'valueD' ) ) It should be like this array( 'AAA' => ( weeks => ( 'week1' => 'valueA', 'week2' => 'valueB' ) ) 'BBB' => ( weeks => ( 'week1' => 'valueC', 'week2' => 'valueD' ) ) ) This can be accomplished by adding some logic to the code that builds the array. I didn't test mac_gyver's solution. It looks like it would work, but I would do it a little differently to make it easier to understand. NOTE: Don't use variable names such as "$array" - give them meaningful names to help you know what they contain and the type of variable. I assume you have a foreach() or some other type of loop, so this may need some tweaking. Other assumptions: 1) The The records are ordered by date. 2) The is that for every employee the same "weeks" exist in the data. If not, some additional logic would be needed. But, this should get you started <?php //Create array to hold the restructured data $payrollAry = array(); //Process the data into a structured array foreach($resultSet as $row) { //If this is the first employee record for this ID, set the top-level array values if(!isset($payrollAry[$row->IdEmployee])) { $payrollAry[$row->IdEmployee] = array( 'name' => $row->employeeName, 'weeks' = array() ); } //Add the weekly data to the sub array (by date) for the current employee $date = date("jS M Y", strtotime($row->Year.'W'.str_pad($row->Week, 2, 0, STR_PAD_LEFT).' +6 days')); $payrollAry[$row->IdEmployee]['weeks'][$date] = "{$row->CurrencySymbol}{$row->Wage}"; } //Create output for the data $wageOutput = ''; //Create the header row output $wageOutput .= "<tr><th> </th>"; //Use the first element in array to get the dates for the header $firstRecord = current($payrollAry); forech($firstRecord['weeks'] as $date => $value) { $wageOutput .= "<th>{$date}</th>"; } $wageOutput .= "</tr>\n"; //Create output for employees foreach($payrollAry as $employeeID => $employeeData) { $wageOutput .= "<tr>\n"; //Add employee name $wageOutput .= "<td>{$employeeData['name']}</td>\n"; //Add employee wages for each week foreach($employeeData['weeks'] as $wages) { $wageOutput .= "<td>{$wages}</td>\n"; } $wageOutput .= "</tr>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/301250-foreach-loop/#findComment-1533213 Share on other sites More sharing options...
andyd34 Posted May 24, 2016 Author Share Posted May 24, 2016 Thank you so much for your help Quote Link to comment https://forums.phpfreaks.com/topic/301250-foreach-loop/#findComment-1533220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.