0

I have json array like bellow:

Array
(
    [total] => 1
    [rows] => Array
    (
        [0] => Array
            (
                [date] => 2014-11-12
                [A] => 1
                [B] => 2
                [C] => 2
                [D] => No
                [E] => New
                [F] => New
                [G] => 1
                [H] => 1
                [I] => 1
                [A] => 1
                [J] => 1
                [B] => 1
            )
        [1] => Array
            (
                [date] => 2014-12-12
                [A] => 1
                [B] => 1
                [C] => 2
                [D] => No
                [E] => New
                [F] => New
                [G] => 2
                [H] => 2
                [I] => 2
                [A] => 2
                [J] => 2
                [B] => 2
            )

    )

)

I want to echo like below:

date      A B C D E F G H I A J B
2014-11-12  1 2 2 No  New New 1 1 1 1 1 1
2014-12-12  1 1 2 No  New New 2 2 2 2 2 2

how i do this thing what is the best way any suggestion.

Kevin
  • 41,694
  • 12
  • 53
  • 70
codemania
  • 1,098
  • 1
  • 9
  • 26

2 Answers2

1

You can just use plain foreach in this case. As for the headers, you may need to use array_keys() for them. Example:

<?php

$array = [ 'total' => 1, 'rows' => [ [ 'date' => '2014-11-12', 'A' => '1', 'B' => '2', 'C' => '2', 'D' => 'No', 'E' => 'New', 'F' => 'New', 'G' => '1', 'H' => '1', 'I' => '1', 'A' => '1', 'J' => '1', 'B' => '1', ], [ 'date' => '2014-12-12', 'A' => '1', 'B' => '1', 'C' => '2', 'D' => 'No', 'E' => 'New', 'F' => 'New', 'G' => '2', 'H' => '2', 'I' => '2', 'A' => '2', 'J' => '2', 'B' => '2', ], ],];
$keys = array_keys(reset($array['rows']));

?>

<table cellpadding="10">
    <thead>
    <tr align="left">
        <?php foreach($keys as $header): ?>
            <th><?php echo $header; ?></th>
        <?php endforeach; ?>
    </tr>
    </thead>
    <tbody>
    <?php foreach($array['rows'] as $row): ?>
        <tr><?php foreach ($row as $value): ?>
            <td><?php echo $value; ?></td>
        <?php endforeach; ?></tr>
    <?php endforeach; ?>
    </tbody>
</table>

Sample Output

Printing in CSV:

$array = [ 'total' => 1, 'rows' => [ [ 'date' => '2014-11-12', 'A' => '1', 'B' => '2', 'C' => '2', 'D' => 'No', 'E' => 'New', 'F' => 'New', 'G' => '1', 'H' => '1', 'I' => '1', 'A' => '1', 'J' => '1', 'B' => '1', ], [ 'date' => '2014-12-12', 'A' => '1', 'B' => '1', 'C' => '2', 'D' => 'No', 'E' => 'New', 'F' => 'New', 'G' => '2', 'H' => '2', 'I' => '2', 'A' => '2', 'J' => '2', 'B' => '2', ], ],];
$data[] = array_keys(reset($array['rows'])); // push header inside
$data = array_merge($data, $array['rows']); // push values inside

$file = fopen('whatevertname.csv', 'w+');
foreach ($data as $value) {
    fputcsv($file, $value);
}
fclose($file);
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

Assuming you are writting csv files using fputcsv() function,

  1. You should first get all array keys using array_keys(). You can get it from the first data row and insert it to a new array (let's name it 'csv_array').
  2. You should now have new array with the first data is array of field.
  3. Then, you should loop all of your data and add it to 'csv_array'.
  4. Write it via fputcsv() function.

your generated 'csv_array' structure should be look like this, after you insert first row data, before using fputcsv()

Array
    (
        [0] => Array
            (
                [0] => date
                [1] => A
                [2] => B
                [3] => C
                [4] => D
                [5] => E
                [6] => F
                [7] => G
                [8] => H
                [9] => I
                [10] => A
                [11] => J
                [12] => B
            )
        [1] => Array
            (
                [0] => 2014-12-12
                [1] => 1
                [2] => 2
                [3] => 2
                [4] => No
                [5] => New
                [6] => New
                [7] => 1
                [8] => 1
                [9] => 1
                [10] => 1
                [11] => 1
                [12] => 1
            )

    )
Moch. Rasyid
  • 366
  • 5
  • 16