Jump to content

jattsurma

Recommended Posts

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:

  1. I need to loop through $bundle_pickup array and find out which arrays are different.
     
  2. 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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.