문제

다음 구조의 다차원 배열이 있고 복제물을 제거하고 싶습니다.예를 들어, [ "금액"]이 두 개의 [ "도시"]에 대해 동일하지만 [ "시간"]은 동일하거나 다릅니다. i는 중복 이며이 노드를 배열에서 제거하려는 것입니다.

다음 예에서는 도시와 양이 노드 1과 동일한 배열에서 완전히 노드 0을 제거하려고합니다. 이들은 Bristol (Bristol, United Kingdom) 및 시간이 다르지만 373은 모두 17 :15 및 17:16.

이 경우와 마찬가지로 시간이 다르면 나중에 제거합니다.

array(8) {
  [0]=>
  array(3) {
    ["time"]=>
    string(5) "17:16"
    ["city"]=>
    string(33) "Bristol (Bristol, United Kingdom)"
    ["amount"]=>
    int(373)
  }
  [1]=>
  array(3) {
    ["time"]=>
    string(5) "17:15"
    ["city"]=>
    string(33) "Bristol (Bristol, United Kingdom)"
    ["amount"]=>
    int(373)
  }
  [2]=>
  array(3) {
    ["time"]=>
    string(5) "17:16"
    ["city"]=>
    string(37) "Wednesbury (Sandwell, United Kingdom)"
    ["amount"]=>
    int(699)
  }
  [3]=>
  array(3) {
    ["time"]=>
    string(5) "17:16"
    ["city"]=>
    string(45) "Wolverhampton (Wolverhampton, United Kingdom)"
    ["amount"]=>
    int(412)
  }
  [4]=>
  array(3) {
    ["time"]=>
    string(5) "17:15"
    ["city"]=>
    string(33) "Swansea (Swansea, United Kingdom)"
    ["amount"]=>
    int(249)
  }
  [5]=>
  array(3) {
    ["time"]=>
    string(5) "17:16"
    ["city"]=>
    string(39) "Watford (Hertfordshire, United Kingdom)"
    ["amount"]=>
    int(229)
  }
  [6]=>
  array(3) {
    ["time"]=>
    string(5) "17:14"
    ["city"]=>
    string(39) "Nottingham (Nottingham, United Kingdom)"
    ["amount"]=>
    int(139)
  }
  [7]=>
  array(3) {
    ["time"]=>
    string(5) "17:13"
    ["city"]=>
    string(31) "Dartford (Kent, United Kingdom)"
    ["amount"]=>
    int(103)
  }
}
.

도움이 되었습니까?

해결책

<?php

$data = array(
    array(
        'time' => '17:16',
        'city' => 'Bristol',
        'amount' => 373,
    ),
    array(
        'time' => '18:16',
        'city' => 'Bristol',
        'amount' => 373,
    ),
    array(
        'time' => '18:16',
        'city' => 'Wednesbury',
        'amount' => 699,
    ),
    array(
        'time' => '19:16',
        'city' => 'Wednesbury',
        'amount' => 699,
    ),
);

$tmp = array();
foreach ($data as $row) {
    $city   = $row['city'];
    $amount = $row['amount'];

    if (!isset($tmp[$city][$amount]) 
        || $tmp[$city][$amount]['time'] < $row['time']) {
        $tmp[$city][$amount] = $row;
    }
}

$data = array();

foreach ($tmp as $cities) {
    foreach ($cities as $city) {
        $data[] = $city;
    }
}

var_dump($data);
.

다른 팁

시도 :

$result = array();
foreach ($array as $place) {
    if (!array_key_exists($place['time'], $result)) {
        $result[$place['time']] = $place;
    }
}
.

2 차원 연관 배열을 만듭니다. 여기서 한 차원이 도시에서 키가 꺼지고 다른 하나는 다음과 같습니다.

$assoc = array();
foreach ($data as $el) {
    $city = $el['city'];
    $amount = $el['amount'];
    if (isset($assoc[$city]) {
        $assoc[$city][$amount] = $el;
    } else {
        $assoc[$city] = array($amount => $el);
    }
}

// Now gather up all the elements back into a single array
$result = array();
foreach ($assoc as $cities)
    foreach ($cities as $city) {
        $result[] = $city;
    }
}
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top