jattsurma Posted October 26, 2012 Share Posted October 26, 2012 Hello Everyone, I am working on a CMS site and I'm stuck very badly with arrays. I don't have much experience with arrays. In fact, this is probably the first time I have used them this heavily. Here is my situation: I have a for each loop with $i++; which takes values from sessions and puts them into arrays: $bundle_pickup[$i]['bundle_ID']=$bundle_ID; $bundle_pickup[$i]['pickup_date']=$_SESSION['bundle'][$bundle_ID]['pickup_date']; $bundle_pickup[$i]['pickup_time']=$_SESSION['bundle'][$bundle_ID]['pickup_time']; $bundle_pickup prints the following total results: Array ( [1] => Array ( [bundle_ID] => 3 [pickup_date] => 2012-11-08 [pickup_time] => 9 AM to 12 PM ) [2] => Array ( [bundle_ID] => 4 [pickup_date] => 2012-11-08 [pickup_time] => 9 AM to 12 PM ) [3] => Array ( [bundle_ID] => 5 [pickup_date] => 2012-11-08 [pickup_time] => 9 AM to 12 PM ) [4] => Array ( [bundle_ID] => 6 [pickup_date] => 2012-11-08 [pickup_time] => 9 AM to 12 PM ) ) Everything so far is good. Here is what I need to do: I need to loop through $bundle_pickup array and find out which arrays are different. I need to get $bundle_pickup['bundle_ID'] of all the unique arrays The idea is, if the customer picks same pickup date and time for all bundles, he only pays one pickup charge. If he chooses multiple pickup dates or times, he will need to pay multiple charges. If two bundles have same pickup dates/times and other two have same ( but different than first two ), the customer will pay two pick up charges. Since this is a CMS site, I will not know the bundle_ID, pickup dates, pickup times and order of the bundles. If anyone can suggest how I can get $bundle_pickup['bundle_ID'] of all the unique arrays, that would be greatly appreciated. Once I have the IDs, I can use them to fetch other data from the database. Thanks in advance. Jatt Surma Quote Link to comment https://forums.phpfreaks.com/topic/269933-looping-through-arrays-and-finding-the-unique-ones/ Share on other sites More sharing options...
Muddy_Funster Posted October 26, 2012 Share Posted October 26, 2012 My suggestion would be to check the values off as you build the $bundle_pickup array and only add a new value and not a duplicate one. This can be done by building a comparison array at the same time and as each value is taken from the session check if the values are in the comparison array first, then if not add the entry to both arrays and check the nest one, if it is in the comparison array discard it. To do it the way you are planning would require some relativly complicated runs thorugh the array to check off the values which would be slightly less efficient. Quote Link to comment https://forums.phpfreaks.com/topic/269933-looping-through-arrays-and-finding-the-unique-ones/#findComment-1387887 Share on other sites More sharing options...
jcbones Posted October 26, 2012 Share Posted October 26, 2012 I would build it a little different, which would help with the sorting issues. $count = 0; $bundle_pickup[$_SESSION['bundle'][$bundle_ID]['pickup_date']][$_SESSION['bundle'][$bundle_ID]['pickup_time']]['bundle' . ++$count] = $bundle_ID; Run that, and you should get an output similar to: Array ( [2012-11-08] => Array ( [9 AM to 12 PM] => Array ( [bundle1] => 3 [bundle2] => 4 [bundle3] => 5 [bundle4] => 6 ) ) ) Does this look something like what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/269933-looping-through-arrays-and-finding-the-unique-ones/#findComment-1388041 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.