我有以下几种价格
$rates = array(
0 => 200,
1 => 150,
2 => 150,
3 => 220,
4 => 300,
5 => 310,
6 => 350,
7 => 325,
8 => 325,
9 => 325,
10 => 300,
);
因此,必须采用以下格式进行格式化:将今天的日期作为第一个日期,并将以下start_date,finish_date,rate添加到assoc数组中,如果有两个或多个连续的日期,则将其推入相同的索引中,但将日期设为finsih汇率将取决于多少天才结束,这是汇率应如何变化的一个示例。
$new_rates = array (
'start_date' => '2018-12-07'
'finish_date' => '2018-12-07'
'rate' => '200',
'start_date' => '2018-12-08'
'finish_date' => '2018-12-09'
'rate' => '150',
'start_date' => '2018-12-10'
'finish_date' => '2018-12-10'
'rate' => '220',
'start_date' => '2018-12-11'
'finish_date' => '2018-12-11'
'rate' => '300',
'start_date' => '2018-12-12'
'finish_date' => '2018-12-12'
'rate' => '310',
'start_date' => '2018-12-13'
'finish_date' => '2018-12-13'
'rate' => '350',
'start_date' => '2018-12-14'
'finish_date' => '2018-12-15'
'rate' => '325',
'start_date' => '2018-12-16'
'finish_date' => '2018-12-16'
'rate' => '300',
);
如您所见,第一个起始日期可以在今天的日期等开始。 任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
您期望的数组有问题。
'start_date' => '2018-12-07'
'finsih_date' => '2018-12-07'
'rate' => '200',
'start_date' => '2018-12-08'
'finsih_date' => '2018-12-09'
'rate' => '150',
您正在重复键start_date,finish_date,rate。因此,如果您尝试分配新的开始日期,则它将覆盖先前的开始日期。 (assoc_array是PHP中的哈希表)
我建议您改用这种结构,
$new_rates = [
[
'start_date' => '2018-12-07'
'finsih_date' => '2018-12-07'
'rate' => '200',
],
[
'start_date' => '2018-12-08'
'finsih_date' => '2018-12-09'
'rate' => '150',
]
]
或
$new_rates = [
200 => [
'start_date' => '2018-12-07'
'finsih_date' => '2018-12-07'
],
150 =>[
'start_date' => '2018-12-08'
'finsih_date' => '2018-12-09'
]
]
答案 1 :(得分:0)
好吧,以为我明白您要完成的工作,希望对您有所帮助-您可能希望针对特定用例对代码进行一些清理/整理,但是可以完成工作。
<?php
$rates = array(200, 150, 150, 220, 300, 310, 350, 325, 325, 325, 300);
$count = count($rates);
$previousRate = null; // check for duplicate rates
$uniqueRates = []; // store for unique rates
$duplicateRates = []; // store for duplicate rates
$duplicateCount = 0;
/* fill the stores with uniqueRates and duplicateRates */
for ($i = 0; $i < $count; $i++) {
$currentRate = $rates[$i]; // take rate
if($currentRate !== $previousRate) { // not a duplicate rate
$duplicateCount = 0;
$uniqueRates[]=$currentRate;
$previousRate = $rates[$i]; // duplicate check store
} else { // duplicate rate
$duplicateCount++;
$duplicateRates[$currentRate] = $duplicateCount; // store duplicate rate
continue;
}
}
$counter = 0;
foreach($uniqueRates as $key => $rate) {
$dateUnique = date("Y-m-d",
mktime(null, null, null,
date("m"), date("d") + $counter, date("Y")));
if(!key_exists($rate, $duplicateRates)) { // not a duplicate rate
$newArr[$counter]['start_date'] = $dateUnique;
$newArr[$counter]['end_date'] = $dateUnique;
$newArr[$counter]['rate'] = $rate;
} else { // duplicate rate
$dateDuplicate = date("Y-m-d",
mktime(null, null, null,
date("m"), date("d") + $counter + 1, date("Y")));
$newArr[$counter]['start_date'] = $dateUnique;
$newArr[$counter]['end_date'] = $dateDuplicate; // adjusted date for duplicate rates
$newArr[$counter]['rate'] = $rate;
$counter++;
}
$counter++;
}
echo '<pre>';
var_dump($newArr);
echo '</pre>';
输出:
array (size=8)
0 =>
array (size=3)
'start_date' => string '2018-12-08' (length=10)
'end_date' => string '2018-12-08' (length=10)
'rate' => int 200
1 =>
array (size=3)
'start_date' => string '2018-12-09' (length=10)
'end_date' => string '2018-12-10' (length=10)
'rate' => int 150
3 =>
array (size=3)
'start_date' => string '2018-12-11' (length=10)
'end_date' => string '2018-12-11' (length=10)
'rate' => int 220
4 =>
array (size=3)
'start_date' => string '2018-12-12' (length=10)
'end_date' => string '2018-12-12' (length=10)
'rate' => int 300
5 =>
array (size=3)
'start_date' => string '2018-12-13' (length=10)
'end_date' => string '2018-12-13' (length=10)
'rate' => int 310
6 =>
array (size=3)
'start_date' => string '2018-12-14' (length=10)
'end_date' => string '2018-12-14' (length=10)
'rate' => int 350
7 =>
array (size=3)
'start_date' => string '2018-12-15' (length=10)
'end_date' => string '2018-12-16' (length=10)
'rate' => int 325
9 =>
array (size=3)
'start_date' => string '2018-12-17' (length=10)
'end_date' => string '2018-12-17' (length=10)
'rate' => int 300