troubleshooting Question

PHP arrays (manipulation, merging...)

Avatar of Lupi05
Lupi05 asked on
2 Comments1 Solution274 ViewsLast Modified:
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:



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.
number = 1
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.
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.

Original $array
[A][4][10][15.00] for A and 10, the highest quantity is 4, thus 15 / 4 * 6 = 22.50
[A][2][20][14.00] for A and 20, the highest quantity is 2, thus 14 / 2 * 6 = 42
[B][2][10][9.00] for B and 10, the highest quantity is 2, thus 9 / 2 * 6 = 27
[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


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros