PHP 的价格突破数量
Price break quantity for PHP
我有一个这样的数组对象
"PriceBreaks": [
{
"Quantity": 1,
"Price": ".10",
"Currency": "USD"
},
{
"Quantity": 5,
"Price": ".76",
"Currency": "USD"
},
{
"Quantity": 10,
"Price": ".42",
"Currency": "USD"
},
{
"Quantity": 25,
"Price": ".75",
"Currency": "USD"
}
],
我想像上面json一样根据数量计算价格。
我的预期输出是这样的
+ Quantity is 1 => price is $ 12.10
+ Quantity is 4 => price is 4 * $ 12.10
+ Quantity is 5 => price is 5 * $ 11.76
+ Quantity is 8 => price is 8 * $ 11.76
如有任何帮助,我们将不胜感激,并提前致谢
这里是你的计算数组,其中数量和总价是stored.As你已经用货币符号存储了价格,在计算之前你需要删除那个字符。
$json = '{
"PriceBreaks": [{
"Quantity": 1,
"Price": ".10",
"Currency": "USD"
}, {
"Quantity": 5,
"Price": ".76",
"Currency": "USD"
}, {
"Quantity": 10,
"Price": ".42",
"Currency": "USD"
}, {
"Quantity": 25,
"Price": ".75",
"Currency": "USD"
}]
}';
$data = json_decode( $json, true );
$calculation = [];
foreach ($data['PriceBreaks'] as $value) {
$calculation [] = [
'quantity' => $value['Quantity'],
'total_price' => $value['Quantity'] * str_replace('$','', $value['Price'])
];
}
var_dump( $calculation );
这是您可以用来获取灵感的另一种方法。详情见评论。
<?php
// Raw JSON
$json = '[
{
"Quantity": 1,
"Price": ".10",
"Currency": "USD"
},
{
"Quantity": 5,
"Price": ".76",
"Currency": "USD"
},
{
"Quantity": 10,
"Price": ".42",
"Currency": "USD"
},
{
"Quantity": 25,
"Price": ".75",
"Currency": "USD"
}
]';
// Convert JSON to array using the quantity as the index.
$offers = [];
// TRUE to convert to associative array.
foreach (json_decode($json, TRUE) as $offer)
{
// Let's get a float for the calculations instead of the string.
$offer['Price'] = +substr($offer['Price'], 1);
// Store as quantity => offer.
$offers[$offer['Quantity']] = $offer;
}
// The number of items to be purchased.
$customerQuantity = 10;
// max(1, ...) will ensure that the key will never go below 1.
// floor($customerQuantity / 5) * 5 to get to the nearest boundary. This assumes that quantities are always a multiple of 5!
echo $offers[max(1, floor($customerQuantity / 5) * 5)]['Price'];
我有一个这样的数组对象
"PriceBreaks": [
{
"Quantity": 1,
"Price": ".10",
"Currency": "USD"
},
{
"Quantity": 5,
"Price": ".76",
"Currency": "USD"
},
{
"Quantity": 10,
"Price": ".42",
"Currency": "USD"
},
{
"Quantity": 25,
"Price": ".75",
"Currency": "USD"
}
],
我想像上面json一样根据数量计算价格。 我的预期输出是这样的
+ Quantity is 1 => price is $ 12.10
+ Quantity is 4 => price is 4 * $ 12.10
+ Quantity is 5 => price is 5 * $ 11.76
+ Quantity is 8 => price is 8 * $ 11.76
如有任何帮助,我们将不胜感激,并提前致谢
这里是你的计算数组,其中数量和总价是stored.As你已经用货币符号存储了价格,在计算之前你需要删除那个字符。
$json = '{
"PriceBreaks": [{
"Quantity": 1,
"Price": ".10",
"Currency": "USD"
}, {
"Quantity": 5,
"Price": ".76",
"Currency": "USD"
}, {
"Quantity": 10,
"Price": ".42",
"Currency": "USD"
}, {
"Quantity": 25,
"Price": ".75",
"Currency": "USD"
}]
}';
$data = json_decode( $json, true );
$calculation = [];
foreach ($data['PriceBreaks'] as $value) {
$calculation [] = [
'quantity' => $value['Quantity'],
'total_price' => $value['Quantity'] * str_replace('$','', $value['Price'])
];
}
var_dump( $calculation );
这是您可以用来获取灵感的另一种方法。详情见评论。
<?php
// Raw JSON
$json = '[
{
"Quantity": 1,
"Price": ".10",
"Currency": "USD"
},
{
"Quantity": 5,
"Price": ".76",
"Currency": "USD"
},
{
"Quantity": 10,
"Price": ".42",
"Currency": "USD"
},
{
"Quantity": 25,
"Price": ".75",
"Currency": "USD"
}
]';
// Convert JSON to array using the quantity as the index.
$offers = [];
// TRUE to convert to associative array.
foreach (json_decode($json, TRUE) as $offer)
{
// Let's get a float for the calculations instead of the string.
$offer['Price'] = +substr($offer['Price'], 1);
// Store as quantity => offer.
$offers[$offer['Quantity']] = $offer;
}
// The number of items to be purchased.
$customerQuantity = 10;
// max(1, ...) will ensure that the key will never go below 1.
// floor($customerQuantity / 5) * 5 to get to the nearest boundary. This assumes that quantities are always a multiple of 5!
echo $offers[max(1, floor($customerQuantity / 5) * 5)]['Price'];