Jump to content

Sifting Arrays - Logic!


Chud37

Recommended Posts

Hello,

 

Okay.  I am writing a booking system that has the option for a family ticket.  There are four days in total, and if the four people (2 adults, 2 children) book all four days, they get the discounted family ticket. Okay.

 

So i've managed to get all the data into two handy arrays, adult[] and child[], and they go a little something like this:

 

adult = array(array("Name" => "Bob", "Age" => "age1", "Days" => "X1234"));

 

The child array is also very similar.  The days need explaining - the X is simly to fill in the first character because i check it with strpos() and it returns 0 if the character is found in the first slot, and the 1 equels the first day and the 2 the second etc.  Its a weekend event, so 1 is friday, 2 is saturday, 3 is sunday.  So it could be X34 for instance and just be sunday and monday.  Both arrays are quite easy to sort though, and along the way I learnt the wonderful method of callback functions which was quite exciting!  Anyway, I now have all this data, and I want to determine the family tickets.

 

So as I mentioned, the 2 adults and 2 children all need to have X1234 in the 'Days' part of the array.  What I originally did was this:

 

if((!count(adults) % 2) && (!count($child) % 2))  && (count($adults)==(count($child)))){perform stuff}

 

But then i thought, hang on a minute, what if there are 4 adults and 2 children?  I need to somehow go through the two different arrays, Pick out 2 of each that qualify, and group them, until it cant be done anymore.  I cant just simply do it in a one big foreach or for loop as I dont know when it ends, the 2 different counts could be quite different.  So what can i Do?

 

I hope I have explained it well.  I really cant get my head around how to achieve this!

 

Thanks,

 

Richard

Link to comment
Share on other sites

You divide by two and round down both the size of the adults and the size of the children arrays.

 

You then use the smaller value to find out how many 'deal' packages there are.

 

You would first need to isolate the values with X1234

Link to comment
Share on other sites

Hmm maybe you're complicating it. Btw the part about the X made me laugh. If the number is in position 0 then you get 0 in return which you evaluate to "false". Use === (3 equal signs) to surely determine it's not a boolean false, then you don't need the X.

 

Would it be possible for you to make one array for all booked people?

 

$people = array(
                 'adults' => 2,
                 'children' => 2,
                 'days' => '1234'
               );

if ($people['adults'] >= 2 && $people['children'] >= 2 && 'days' == '1234')
    echo 'You\'re getting a family discount!';

Link to comment
Share on other sites

aha! i didnt think of the ===.  But i cant be bothered now to go through and change it all! And that might work, Im just sick of creating new arrays all the time, just to figure out this one thing. 

 

@SilkFire  - Thats not a bad idea in theory but, I cannot just find out the answer with >= 2  because Lets say i have 5 adults and 2 children, then It could potentially work out 2 families.  I need unique grouping somehow of 2 individual adults to 2 individual children.  If there is a booking of say 20 people, And some of them book full weekends and such, I want it to work out how many groups of pairs of adults there are with full bookings, and how many groups of pairs of children there are with full bookings, and then give me the amount of families coming.

 

All this could be solved really by asking my boss if I can just put 1 family discount per booking on the booking page lol.  But i dont want to give up that easily :P

Link to comment
Share on other sites

Hey chud, try this code. It subtracts 2 from adults and 2 from children until either reaches zero then counts the discounts. A full family is 2 adults + 2 children, right?

 

$people = array(
                 'adults' => 3,
	 'children' => 6,
	 'days' => '1234'
       );
			   
$family_discounts = 0;

if ($people['days'] === '1234' && $people['adults'] >= 2 && $people['children'] >= 2) {
   while ($people['adults'] >= 2 && $people['children'] >= 2) {
      $people['adults'] -= 2;
      $people['children'] -= 2;

      $family_discounts++;
   }
}

echo $family_discounts;

 

This will give 1 family discount as result. Am I thinking right? I don't know how your original array looks like or how you're creating it?

Why are you mixing all bookings together?

Link to comment
Share on other sites

i showed my original array for adult[] in the first post, and the child is the same structure.  But actually, thank you very much, I think i can work with this.  I'll take what you wrote and modify if it so it fits, but i think you've got the answer there.

 

Thanks! :D

Link to comment
Share on other sites

I've done it.  Silkfire you helped, but i ended up kinda writing a new procedure in the end.  I said i'd show my results so here tis.

 

$familyAdults = array(); $familyChilds = array();
		$familyTicket=0;

		foreach($adult as $key => $data)
			if($data['Days']=="X1234") {array_push($familyAdults,$data['Name']);}
		foreach($child as $key => $data)
			if($data['Days']=="X1234") {array_push($familyChilds,$data['Name']);}

		$family = array();
		$subArray = array();
		for($x=0;$x<count($familyAdults);$x+=2)
		{	if(isset($familyAdults[$x])) 	{array_push($subArray,$familyAdults[$x]);}
			if(isset($familyAdults[$x+1])) 	{array_push($subArray,$familyAdults[$x+1]);}
			if(isset($familyChilds[$x])) 	{array_push($subArray,$familyChilds[$x]);}
			if(isset($familyChilds[$x+1])) 	{array_push($subArray,$familyChilds[$x+1]);}					
			echo "COUNT: " .  count($subArray) . "<br>";
			if(count($subArray)==4) {$familyTicket++; array_push($family,$subArray); $subArray = array();}
		}

		echo "Adult - (c)" . count($familyAdults) . "<br>";
		pPrint($familyAdults);
		echo "Child - (c)" . count($familyChilds) . "<br>";
		pPrint($familyChilds);
		echo "FAMILY<br>";
		pPrint($family);
		echo "FAMILY TICKET: " . $familyTicket . "<br>";

 

basically, I got all the qualifying Adults and Children in to $familyAdults[] and $familyChilds[].  Then, I step through them in 2's in the Adults, and form an array called $family.  Then I have all the names of the Family in one array in groups of four.

 

Hooray.

 

If you can simplify this code, and make it better, I'd love to know how and why because Im always willing to learn.

 

:)  Thanks for your help guys.

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.