2014-04-14 2 views
1

다음 구조의 다차원 배열이 있고 중복을 제거하려고합니다. 예를 들어 [ "amount"]가 두 [ "cities"]에 대해 동일하지만 [ "time"]이 같거나 다르면이 수가 중복되어 배열에서이 노드를 제거하려고합니다.두 값이 일치하는 경우 PHP에서 다차원 연관 배열에서 중복을 제거합니다.

다음 예제에서는 도시와 금액이 노드 1과 동일하기 때문에 배열에서 노드 0을 완전히 제거하고 싶습니다. 이들은 브리스톨 (Bristol, United Kingdom)과 373입니다. : 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) 
    } 
} 

답변

1
<?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); 
+0

이 덕분에 나는 $ tmp [$ city] [$ amount] [ 'time'] <$ row [ 'time']> $ tmp [$ city] [ 'time']> $ row [ 'time'] 그래서 이전 시간과 th를 유지했습니다. 나중에. –

1

한 차원 도시를에 키가 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; 
    } 
} 
3

이 시도 :

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