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

    Due to the shape of the array and the required layout of the data export, I've solved this by parsing the arrays and adding the outputs to a variable, along with the required commas and \n characters, which works well.

    Any comments still appreciated though.

      4 days later

      Haha - I sort of meant on the export issue rather than the ASCII art, but I'm glad you like it... !

      I have a new thing to acheive with this data array now. I can successfully export it to a CSV (in the form of the ASCII art layout) but now I need to export it in a different shape.

      I need to get the day/date down the side rather than along the top, and obviously the data will be re-postioned to suit.

      Is there a 'proper' approach to this? Or just parse and re-parse the array, writing a new one?

        Unfortunately nested arrays often have more dimensions than you can express in a 2-d spreadsheet so I don't think there's any generalized solution to this problem -- I certainly don't know of one.

        As for switching rows for columns, it generally involves rearranging the loops you use to deal with your original array and may require that you define a new array and then loop through that instead.

          2 months later

          Thanks again. Your explanation of nested arrays often having more dimensions than you can express in a 2D spreadsheet was spot on and really helped the working of the coded solution.

            Write a Reply...