英文:
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] => 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>';
}
}
答案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( '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
><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( '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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论