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 Quote Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/ Share on other sites More sharing options...
Solution CroNiX Posted June 19, 2014 Solution 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]"; } Quote 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. Quote 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. Quote 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" Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/289210-csv-group-duplication/#findComment-1482913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.