Patashu & Nikola 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
나는 array ("June 4"=> array (30,35))와 같은 배열을 재구성 할 것이고 ... –
당신은 이미 이것의 실제 예제 코드를 가지고 있습니까? 당신이 가진 방식은 그것이 현재의 구조와 함께 당신이 원하는 방식으로 작동하지 않을 것입니다. 날짜와 값은 어떻게 시작합니까? –