Jane Doe
Jane Doe

Reputation: 302

Using php laravel code: Leave the cell (exported excel) empty if an array is duplicate

I am using php laravel. I'm trying to export the data into excel .csv file with unique data or leaving the cell empty instead of duplicating the data. What filter should I use to do that? To be clear I will show some illustration
This is an example of the Excel View with the data from the array which is exported using php:

+--------+------------+-----------+
| Group  | First Name | Last Name |
+--------+------------+-----------+
| Group1 | John       | Doe       |
+--------+------------+-----------+
| Group1 | Jane       | Doe       |
+--------+------------+-----------+
| Group2 | John       | Smith     |
+--------+------------+-----------+
| Group2 | Jane       | Smith     |
+--------+------------+-----------+

This is an example of Excel View that I need. You can see that the duplicating groups are hidden:

+--------+------------+-----------+
| Group  | First Name | Last Name |
+--------+------------+-----------+
| Group1 | John       | Doe       |
+--------+------------+-----------+
|        | Jane       | Doe       |
+--------+------------+-----------+
| Group2 | John       | Smith     |
+--------+------------+-----------+
|        | Jane       | Smith     |
+--------+------------+-----------+

And this is the code that I've been using:

public function getDashboardBookingsReport2($account_alias, $group_alias){
    header       (...)

        $file = fopen('php://output', 'w');


        fputcsv($file, array('Group Name', 'First Name', 'Last Name'));

        $table = (...)

        $rowed[] = array(
            $egroup->name,     // Group
            $user->first_name, // First Name
            $user->last_name,  // Last Name
                        );
                    }

                }
            }
                (...)

        $data = $rowed;
        uasort($data, function($a, $b) {
            return strcmp($a[0], $b[0]);
        });
        foreach ($data as $row)
        {
            fputcsv($file, $row);
        }

        exit();
    return Redirect::to( $group_alias.'/app/dash' );
}

Upvotes: 0

Views: 701

Answers (1)

Paras
Paras

Reputation: 9465

Add this code just before foreach ($data as $row) and after uasort($data, function($a, $b):

for($i = count($data) - 1; $i > 0; $i--) {
    if($data[$i][0] === $data[$i - 1][0]) {
        $data[$i][0] = "";
    }
}

Upvotes: 1

Related Questions