這是你的問題的一個可能的解決方案,但我會建議重新建設,因爲Patashu &尼古拉R說。如果需要更精確的重新建設推薦方案新增指標:
$untrimmed = [["June 4",30],["June 4",35],["June 5",46],["June 5",38.33],["June 5",12]];
$trimmed = stripDates($untrimmed);
function stripDates($dates) {
foreach($dates as $key=>$date) {
if ($key>0) {
if ($date[0] === $dates[$key-1][0]) {
$dates[$key][0] = "";
} else if($dates[$key-1][0] === "") {
for ($i = $key-1; $i > -1; $i--) {
if ($date[0] === $dates[$i][0]) $dates[$key][0] = "";
if ($dates[$key] != "") break;
}
}
}
}
return $dates;
}
// Note: This would require dates to be added chronically
//Output: ["June 4",30],["",35],["June 5",46],["",38.33],["",12]
我建議是這樣的:
$unconstructed = [["June 4",30],["June 4",35],["June 5",46],["June 5",38.33],["June 5",12]];
$constructed = constructAssoc($unconstructed);
function constructAssoc($dates) {
$constructed = array();
foreach($dates as $index=>$date) {
if (!array_key_exists($date[0], $constructed)) {
$constructed[$date[0]] = array("index"=>$index, "value"=>$date[1]);
} else {
array_push($constructed[$date[0], ["index"=>$index,"value"=>$date[1]]);
}
}
return $constructed;
}
//Output: ["June 4"=> [["index"=>0, "value"=>30], ["index"=>1, "value"=>35]], "June 5"=>[["index"=>2, "value"=>46], ["index"=>3, "value"=>38.33], ["index"=>4, "value"=>12]]]
注意。
創建從日期標籤到值的關聯數組?這樣,如果添加兩個具有相同日期標籤的條目,則最終只有一個條目,因此只有一個值。 – Patashu
我會重組陣列(「6月4日」=>數組(30,35)),等等...... –
你已經有這個實際的例子代碼。你擁有的方式不會像你現在的結構那樣按照你想要的方式工作。你如何得到開始的日期和值? –