...

View Full Version : Can't seem to figure out array loop...



loki421
11-19-2011, 03:27 PM
Hi guys and gals!

Must be having that wekend feeling because I just can't seem to figure out the best way to do this..

So here's what I'm struggling with, I have a multi-dimentional array that looks like this:



Array
(
[0] => Array
(
[id] => 2
[qty] => 1
[size] => XL
[def] => prod_1
)
[1] => Array
(
[id] => 5
[qty] => 1
[size] => XL
[def] => prod_2
)
[2] => Array
(
[id] => 10
[qty] => 1
[size] => L
[def] => prod_7
)
)


Basically that's a very simple version of a cart. What I'm trying to do is apply a discount if two products the same size are found. So in the above example there are two products with the same size (XL), so this would qualify.

If another product was added with a size of large (L) then this would also qualify for a discount as there would be two products with size large in the array as well as two XL products. Hope this makes sense so far?

Ok, so I've tried all sorts of for, foreach and while loops but none are giving me the expected results. I've looked at maybe using array_intersect() but don't think that would work on a multi-demntional array such as this.

Does anyone have any pointers they could offer? I would be very gratefull to see how you peeps would tackle this kind of problem.

Looking forward to any replys :)

sunfighter
11-19-2011, 07:11 PM
This is one way. You can use the $xlDiscount in code below to give out your discounts. The echos are for you to see the results.


<?php
$cart = array( array(2, 1 , 'XL','prod_1'),
array(5, 1 , 'XL','prod_2'),
array(10, 1 , 'L','prod_7')
);

$sDiscount = 0;
$mDiscount = 0;
$lDiscount = 0;
$xlDiscount = 0;
$xxlDiscount = 0;

for($row = 0; $row < count($cart); $row++)
{
if ($cart[$row][2] == 'S') {$sDiscount++;}
if ($cart[$row][2] == 'M') {$mDiscount++;}
if ($cart[$row][2] == 'L') {$lDiscount++;}
if ($cart[$row][2] == 'XL') {$xlDiscount++;}
if ($cart[$row][2] == 'XXL') {$xxlDiscount++;}
}

echo $sDiscount,'<br>';
echo $mDiscount,'<br>';
echo $lDiscount,'<br>';
echo $xlDiscount,'<br>';
echo $xxlDiscount,'<br>';
?>

loki421
11-19-2011, 07:35 PM
This is one way. You can use the $xlDiscount in code below to give out your discounts. The echos are for you to see the results.


<?php
$cart = array( array(2, 1 , 'XL','prod_1'),
array(5, 1 , 'XL','prod_2'),
array(10, 1 , 'L','prod_7')
);

$sDiscount = 0;
$mDiscount = 0;
$lDiscount = 0;
$xlDiscount = 0;
$xxlDiscount = 0;

for($row = 0; $row < count($cart); $row++)
{
if ($cart[$row][2] == 'S') {$sDiscount++;}
if ($cart[$row][2] == 'M') {$mDiscount++;}
if ($cart[$row][2] == 'L') {$lDiscount++;}
if ($cart[$row][2] == 'XL') {$xlDiscount++;}
if ($cart[$row][2] == 'XXL') {$xxlDiscount++;}
}

echo $sDiscount,'<br>';
echo $mDiscount,'<br>';
echo $lDiscount,'<br>';
echo $xlDiscount,'<br>';
echo $xxlDiscount,'<br>';
?>

Mmm, that could be one way, but that's just going to give me a count of how many matches there is is the array.

What I'm trying to do (and have been trying all day! lol) is to check to see if there's "groups" of two. So basically say a large product (L) is 1.99, two large products would qualify for a discount, making the total cart price only 2.50, for example.

What I've got so far is an array of sizes with keys inside them, kind like your code above... ish...

This is a method from my cart class that receives my entire cart as above (initial post)


public function get_same_size_products($arr){

if(!empty($arr)){

$new_arr = array();

foreach($arr as $k => $array){

switch($array['size'])
{

case 'XL':
$new_arr['XL'][] = $k;
break;

case 'L':
$new_arr['L'][] = $k;
break;

case 'R':
$new_arr['R'][] = $k;
break;

}

}

return $new_arr;

}else{

return false;

}

}


So that gives me a bunch of keys for which items in the cart can qualify for discounts. The above would give me something like this:



Array
(
[XL] => Array
(
[0] => 0
[1] => 1
[2] => 3
[3] => 4
[4] => 5
)

[L] => Array
(
[0] => 2
)

)


Now array[L] would not qualify, but array[XL] would have two discounts, as there are two groups of two sizes the same plus one left over, does that makes sense?

So ideally I'd like to get something like:



Array
(
[0] => Array
(
[0] => 0
[1] => 1
)

[1] => Array
(
[0] => 3
[2] => 4
)

)


Because I could then target each one of those keys in the cart array and reduce the 'price' value so I've applied my discount.

I really think I'm over complicating it though as I've been on this all day and it just seems like such a simple problem and I just can't figure it out... very frustrating..... Hence why I thought I'd post here to see if some fresh eyes might see something I'm not! lol.

Thanks for your time though, I'd appreciate any input anyone might have on this.... :)

Thanks.

sunfighter
11-19-2011, 08:16 PM
Are you saying that if $xlDiscount is 2 or better you can't figure out how add discout to items with size = XL?

Adee
11-19-2011, 08:46 PM
I think you can use this? Not sure if it makes sense for what you're trying to do tho..

It will see if there are two items of the same size in the $test array and then act accordingly.. it doesn't take into account if say id: 1 is a size of extra large but has a qty greater than one but I'm sure you can work that into it somewhere.


<?php


$test = array(
array('id'=>1,'qty'=>1,'size'=>'XL','def'=>'prod_1'),
array('id'=>2,'qty'=>3,'size'=>'XL','def'=>'prod_4'),
array('id'=>3,'qty'=>1,'size'=>'S','def'=>'prod_3'),
array('id'=>4,'qty'=>1,'size'=>'XXL','def'=>'prod_12'),
array('id'=>5,'qty'=>3,'size'=>'XXL','def'=>'prod_44'),
array('id'=>6,'qty'=>1,'size'=>'S','def'=>'prod_39'),
array('id'=>7,'qty'=>1,'size'=>'XL','def'=>'prod_41'),
array('id'=>8,'qty'=>3,'size'=>'S','def'=>'prod_34'),
array('id'=>9,'qty'=>1,'size'=>'S','def'=>'prod_65')
);

$sDiscount = 0;
$mDiscount = 0;
$lDiscount = 0;
$xlDiscount = 0;
$xxlDiscount = 0;

foreach($test as $c)
{
$sizes[$c['size']][] = $c['size'];
}

foreach($sizes as $disc)
{
if(count($disc) > 1)
{
if($disc[0] == "XL") { echo "<P>XL Discount found.</p>"; $xlDiscount++; }
if($disc[0] == "XXL") { echo "<P>XXL Discount found.</p>"; $xxlDiscount++; }
if($disc[0] == "S") { echo "<P>S Discount found.</p>"; $sDiscount++; }
if($disc[0] == "M") { echo "<P>M Discount found.</p>"; $mDiscount++; }
if($disc[0] == "L") { echo "<P>L Discount found.</p>"; $lDiscount++; }

}
}



?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum