We help IT Professionals succeed at work.

PHP arrays (manipulation, merging...)

Lupi05
Lupi05 asked
on
271 Views
Last Modified: 2011-10-19
Hi experts,

spent most of my day trying to find a solution for this, no avail.
Should be easy for somebody who has got a bit of experience with PHP arrays, I think.

In PHP, I have a two dimensional array:

$array[0]=array("A","1","10","5.00")
$array[1]=array("A","2","10","9.00")
$array[2]=array("A","4","10","15.00")
$array[3]=array("A","1","20","8.00")
$array[4]=array("A","2","20","14.00")
$array[5]=array("B","1","10","5.00")
$array[6]=array("B","2","10","9.00")
$array[7]=array("B","1","20","8.00")
$array[8]=array("B","2","20","14.00")
$array[9]=array("C","1","10","5.00")
$array[10]=array("C","1","20","8.00")

[A][1][10][5.00]
[A][2][10][9.00]
[A][4][10][15.00]
[A][1][20][8.00]
[A][2][20][14.00]
[B][1][10][5.00]
[B][2][10][9.00]
[B][1][20][8.00]
[B][2][20][14.00]
[C][1][10][5.00]
[C][1][20][8.00]

Now, this array holds quantities of a product (e.g. 1x10, 2x10, 4x10, 1x20, 2x20) and prices. Of course, the prices would be different, I simplified the example for EE though.

I want to be able to chose a quantity (1, 2, 4 or 6) and have all the results which have that quantity returned, or, if they don't exist, calculated.
Example:
number = 1
[A][1][10][5.00]
[A][1][20][8.00]
[B][1][10][5.00]
[B][1][20][8.00]
[C][1][10][5.00]
[C][1][20][8.00]
This one is very easy because all these results do exist in $array, I can simply use array_filter or just use a foreach ($array as $x) loop and only keep those results where $x[1] == 1. I know how to do that.

number = 6 (LET'S CALL THIS ARRAY $number6)
As you can see, there are no results in $array that have a quantity of 6. They have to be calculated.
[A][6][10][22.50]
[A][6][20][42.00]
[B][6][10][27.00]
[B][6][20][42.00]
[C][6][10][30.00]
[C][6][20][48.00]
Now this wouldn't be very difficult either (simply take the 1 values and multiply by 6).
HOWEVER, since price discounts can apply, for each letter (A, B, C) the price of the highest quantity in the original $array has to be divided by itself and then multiplied by 6.

Example:
Original $array
[A][1][10][5.00]
[A][2][10][9.00]
[A][4][10][15.00] for A and 10, the highest quantity is 4, thus 15 / 4 * 6 = 22.50
[A][1][20][8.00]
[A][2][20][14.00] for A and 20, the highest quantity is 2, thus 14 / 2 * 6 = 42
[B][1][10][5.00]
[B][2][10][9.00] for B and 10, the highest quantity is 2, thus 9 / 2 * 6 = 27
[B][1][20][8.00]
[B][2][20][14.00] for B and 20, the highest quantity is 2, thus 14 / 2 * 6 = 42
[C][1][10][5.00] for C and 10, the highest quantity is 1, thus 5 / 1 * 6 = 30
[C][1][20][8.00] for C and 20, the highest quantity is 1, thus 8 / 1 * 6 = 48

This returns the array $number6 (as above).

Thus, what has to be done is always the same:

Take the original $array. Depending on the number $n that is chosen (1, 2, 4 or 6), go and find the one row (subarray) with the highest quantity (field [1]) that is smaller than or equal to $n for every possible combination of the fields [0] and [2] (i.e. A and 10, A and 20, B and 10, B and 20, C and 10, C and 20). Divide the price (field [3]) of this row/subarray by quantity (field [1]) and multiply by $n. Then return a new array with the results.

Thank you for your help.

Cheers, Chris

Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi, thanks so much for putting in the effort and helping me solve the issue... To be honest, it looks a whole lot more complex than I thought it'd be! Anyway, it's working, so I just have to integrate it into my project. Cheers, Chris
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.