Hi all
I've got an array stored in a session variable that looks like this under print_r :
Array
(
[0] => Array
(
[ONE] => Array
(
[0] => Array
(
[al] => 1500
[bd] => 90
[rem] => 1410
[ns] => 80
[nonns] => 10
[total] => 90
[date] => 01-06-2011
[day] => W
)
[1] => Array
(
[al] => 1500
[bd] => 100
[rem] => 1400
[ns] => 90
[nonns] => 10
[total] => 100
[date] => 02-06-2011
[day] => T
)
)
)
[1] => Array
(
[TWO] => Array
(
[0] => Array
(
[al] => 2400
[bd] => 2170
[rem] => 230
[ns] => 620
[nonns] => 1550
[total] => 2170
[date] => 01-06-2011
[day] => W
)
[1] => Array
(
[al] => 2400
[bd] => 2240
[rem] => 160
[ns] => 680
[nonns] => 1560
[total] => 2240
[date] => 02-06-2011
[day] => T
)
)
)
[2] => Array
(
[THREE] => Array
(
[0] => Array
(
[al] => 1800
[bd] => 1400
[rem] => 400
[ns] => 290
[nonns] => 1110
[total] => 1400
[date] => 01-06-2011
[day] => W
)
[1] => Array
(
[al] => 1800
[bd] => 1570
[rem] => 230
[ns] => 350
[nonns] => 1220
[total] => 1570
[date] => 02-06-2011
[day] => T
)
)
)
[3] => Array
(
[FOUR] => Array
(
[0] => Array
(
[al] => 1800
[bd] => 1400
[rem] => 400
[ns] => 450
[nonns] => 950
[total] => 1400
[date] => 01-06-2011
[day] => W
)
[1] => Array
(
[al] => 1800
[bd] => 1580
[rem] => 220
[ns] => 520
[nonns] => 1060
[total] => 1580
[date] => 02-06-2011
[day] => T
)
)
)
)
I'm using a function to read arrays and echo them as a CSV, but it only works with flat arrays:
function array_to_data($array, $header_row = true, $col_sep = ",", $row_sep = "\n", $qut = '"')
{
if (!is_array($array) or !is_array($array[0])) return false;
//Header row.
if ($header_row)
{
foreach ($array[0] as $key => $val)
{
//Escaping quotes.
$key = str_replace($qut, "$qut$qut", $key);
$output .= "$col_sep$qut$key$qut";
}
$output = substr($output, 1)."\n";
}
//Data rows.
foreach ($array as $key => $val)
{
$tmp = '';
foreach ($val as $cell_key => $cell_val)
{
//Escaping quotes.
$cell_val = str_replace($qut, "$qut$qut", $cell_val);
$tmp .= "$col_sep$qut$cell_val$qut";
}
$output .= substr($tmp, 1).$row_sep;
}
return $output;
}
I'm trying to get the resulting imported CSV to look like this in Excel (excuse the ASCII art!)
+-------+----------+----------+----------+----
| | day date | day date | day date |
+-------+----------+----------+----------+----
| ONE | ns | ns | ns |
+-------+----------+----------+----------+----
| | nonns | nonns | nonns |
+-------+----------+----------+----------+----
| | total | total | total |
+-------+----------+----------+----------+----
| TWO | ns | ns | ns |
+-------+----------+----------+----------+----
| | nonns | nonns | nonns |
+-------+----------+----------+----------+----
| | total | total | total |
+-------+----------+----------+----------+----
I haven't shown all the data in the spreadsheet for brevity purposes.
Can anyone advise the best way of appoaching this please? I think probably cycling through the arrays and echoing an appropriate number of commas between them might be possible, but I'm sure someone here has a much more elegant way!
Thanks