thecase Posted June 19, 2014 Share Posted June 19, 2014 Hi I am reading in data from a csv file if (($handle = fopen("data.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "User Name: $data[0]"; echo "Booking IDs: $data[1]"; } echo "<hr>"; The problem is some of the usernames are duplicated so get outputted the same user name 20 times but with different IDs, obviously but I am unsure how to group them together. I can't seem to figure out how to write if the next username is the same as current then just add another ID not go through the entire loop. Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/ Share on other sites More sharing options...
CroNiX Posted June 19, 2014 Share Posted June 19, 2014 $last_user = ''; //track the last user while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $user = $data[0]; if ($user !== $last_user) //if the current user isn't the same, store as last user and display name { $last_user = $user; echo "User Name: $user"; } echo "Booking IDs: $data[1]"; } Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/#findComment-1482908 Share on other sites More sharing options...
DavidAM Posted June 19, 2014 Share Posted June 19, 2014 You have to keep track of the last one printed: $lastUser = null; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($data[0] != $lastUser) { echo "User Name: $data[0]"; $lastUser = $data[0]; } echo "Booking IDs: $data[1]"; } echo "<hr>";Of course, this would depend on the user records being grouped together in the input. If they are not, you could load all of the data into a multi-dimensional array. Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/#findComment-1482909 Share on other sites More sharing options...
mac_gyver Posted June 19, 2014 Share Posted June 19, 2014 you probably shouldn't assume that the data in the csv is in any particular order and pre-processes the data by storing it in an array using the username as an array key and storing each booking id as a sub-array under the username it belongs with. Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/#findComment-1482910 Share on other sites More sharing options...
CroNiX Posted June 19, 2014 Share Posted June 19, 2014 @mac_gyver, true, but that's what he specifically asked: "the next username is the same as current" Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/#findComment-1482911 Share on other sites More sharing options...
thecase Posted June 19, 2014 Author Share Posted June 19, 2014 Thanks CroNiX and DavidAM you were both correct I could only mark one. Works perfectly. I will take into consideration about using the array keys Thanks. Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/#findComment-1482913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.