使用PHP根据另一个值获取数组中的特定数值。

huangapple go评论59阅读模式
英文:

How to get specific value in an array based on the another value using PHP

问题

以下是翻译好的部分:

我在获取数组中的正确值时遇到了问题。我有一个具有moq和costunit键的数组。如果给定的数量大于或等于moq,我想获取costunit。我有以下数组。

示例 1:如果给定的数量是25000,那么成本单位将显示为0.33。

示例 2:如果给定的数量是1230,那么成本单位将显示为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>';
  }
}

注意:这部分内容是关于在给定数量大于或等于moq时获取costunit的问题的代码示例。

英文:

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] =&gt; 1000
    [costunit] =&gt; 0.44
)
Array
(
    [moq] =&gt; 20000
    [costunit] =&gt; 0.33
)
Array
(
    [moq] =&gt; 30000
    [costunit] =&gt; 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( &#39;moq&#39;=&gt; 1000,  &#39;costunit&#39;=&gt; 0.44 ),
  array( &#39;moq&#39;=&gt; 20000, &#39;costunit&#39;=&gt; 0.33 ),
  array( &#39;moq&#39;=&gt; 30000, &#39;costunit&#39;=&gt; 0.30 ),
);
$get_quantity = 30000;
foreach($get_prices as $get_price){

  if($get_price[&#39;moq&#39;] &gt;= $get_quantity){
    echo &#39;&lt;pre&gt;&#39;;
        print_r($get_price[&#39;costunit&#39;]);
    echo &#39;&lt;/pre&gt;&#39;;
   }
}

答案1

得分: 2

以下是代码部分的翻译:

All I think you need is a break in the loop so it stops when it finds the first valid value
我认为你只需要在循环中加入一个break语句,以便在找到第一个有效值时停止。

RESULT
结果

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
如果给定的数量是25000,那么成本单位将显示为0.33

If the given qty is 1230 then the cost unit would be displayed is 0.44
如果给定的数量是1230,那么成本单位将显示为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 occurrence of the array.
那么这段代码将实现这一点,但是,你需要决定要将$prev_cu设置为什么值,以便在数组的第一次出现中,如果$get_quantity小于moq,则获得正确的值。

$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 cost unit 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;

以上是你提供的代码部分的翻译。

英文:

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( &#39;moq&#39;=&gt; 1000, &#39;costunit&#39;=&gt; 0.44 ),
  array( &#39;moq&#39;=&gt; 20000,&#39;costunit&#39;=&gt; 0.33 ),
  array( &#39;moq&#39;=&gt; 30000, &#39;costunit&#39;=&gt; 0.30 )
);
$get_quantity = 1250;
foreach($get_prices as $get_price){

  if($get_price[&#39;moq&#39;] &gt;= $get_quantity){
    echo &#39;&lt;pre&gt;&#39;;
        print_r($get_price[&#39;costunit&#39;]);
    echo &#39;&lt;/pre&gt;&#39;;
    break;
   }
}

RESULT

&lt;pre&gt;0.33&lt;/pre&gt;

>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
><br>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( &#39;moq&#39;=&gt; 1000, &#39;costunit&#39;=&gt; 0.44 ),
    array( &#39;moq&#39;=&gt; 20000,&#39;costunit&#39;=&gt; 0.33 ),
    array( &#39;moq&#39;=&gt; 30000, &#39;costunit&#39;=&gt; 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 &lt; $get_price[&#39;moq&#39;] ) {
        break;
    }
    if ($get_quantity == $get_price[&#39;moq&#39;] ) {
        $prev_cu = $get_price[&#39;costunit&#39;];
        break;
    }
    $prev_cu = $get_price[&#39;costunit&#39;];
}
echo $prev_cu;

huangapple
  • 本文由 发表于 2023年2月6日 17:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定