0

I am having a problem getting the right value in the array. I have an array that has a key of moq and costunit. I want to get the costunit if the given quantity is greater than or equal to the moq. I have the following array.

Array
(
    [moq] => 1000
    [costunit] => 0.44
)
Array
(
    [moq] => 20000
    [costunit] => 0.33
)
Array
(
    [moq] => 30000
    [costunit] => 0.30
)

Example 1: if the given qty is 25000 then the cost unit would be displayed is 0.33

Example 2: if the given qty is 1230 then the cost unit would be displayed is 0.44

$get_prices = array(
  array( 'moq'=> 1000,  'costunit'=> 0.44 ),
  array( 'moq'=> 20000, 'costunit'=> 0.33 ),
  array( 'moq'=> 30000, 'costunit'=> 0.30 ),
);
$get_quantity = 30000;
foreach($get_prices as $get_price){

  if($get_price['moq'] >= $get_quantity){
    echo '<pre>';
        print_r($get_price['costunit']);
    echo '</pre>';
   }
}
7
  • I think the code is doing exactly what you requirement specified. So maybe your spec is what is wrong Commented Feb 6, 2023 at 9:45
  • The code posted in the question does not compile. It needs commas after the values in the array. Commented Feb 6, 2023 at 9:47
  • 2
    "if the given qty is 1230 then the cost unit would be displayed is 0.44" - why? You said, "if the moq is greater or equal to the given quantity" - the moq of 1000 is not greater than your given quantity of 1230. Commented Feb 6, 2023 at 9:49
  • Sorry, I've just updated my question. What I want is to get the costunit of moq is above or equal the given quantity.Example 1: if the given qty is 25000 then the cost unit would be displayed is 0.33 Commented Feb 6, 2023 at 10:35
  • No, both of your examples do not match your verbal specification! Please take a breath and have a second look at what you want the code to do. Commented Feb 6, 2023 at 10:48

1 Answer 1

2

All I think you need is a break in the loop so it stops when it find the first valid value

$get_prices = array(
  array( 'moq'=> 1000, 'costunit'=> 0.44 ),
  array( 'moq'=> 20000,'costunit'=> 0.33 ),
  array( 'moq'=> 30000, 'costunit'=> 0.30 )
);
$get_quantity = 1250;
foreach($get_prices as $get_price){

  if($get_price['moq'] >= $get_quantity){
    echo '<pre>';
        print_r($get_price['costunit']);
    echo '</pre>';
    break;
   }
}

RESULT

<pre>0.33</pre>

NOTE: I think your examples are wrong, have another look at what you said because it seems it is exactly what the code did, apart from not stopping when it found the first valid value


If as you say these are the rules

If the given qty is 25000 then the cost unit would be displayed is 0.33
If the given qty is 1230 then the cost unit would be displayed is 0.44

Then this code will do that, however, you will have to decide what to set $prev_cu to so you get the right value if the $get_quantity is less than the moq in the first occurance of the array.

$get_prices = array(
    array( 'moq'=> 1000, 'costunit'=> 0.44 ),
    array( 'moq'=> 20000,'costunit'=> 0.33 ),
    array( 'moq'=> 30000, 'costunit'=> 0.30 )
);

$get_quantity = 25000;
// set a costunit to use if qty is less than occ 0 of array
$prev_cu = 0;   

foreach ($get_prices as $get_price) {
    if ($get_quantity < $get_price['moq'] ) {
        break;
    }
    if ($get_quantity == $get_price['moq'] ) {
        $prev_cu = $get_price['costunit'];
        break;
    }
    $prev_cu = $get_price['costunit'];
}
echo $prev_cu;
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I've just updated my question. What I want is to get the costunit of moq is above or equal the given quantity.Example 1: if the given qty is 25000 then the cost unit would be displayed is 0.33
Look at the requirement!! The costunit that matches that requirement of a moq GREATER THAN OR EQUAL TO 25000 is the moq of 30000 which is 0.30
@NeverStopLearning Get your specifications right.
I have added a second set of code to match the examples you give

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.