1

I have a multidimensional array of the following structure and I want to remove duplicates from it. For example if the ["amount"] is the same for two ["cities"] but the ["time"] is the same or different then I count this is a duplicate and want to remove this node from the array.

In the following example I want to remove completely node 0 from the array as the city and amount are the same as node 1. They are both Bristol (Bristol, United Kingdom) and 373 Even though the time is different being 17:15 and 17:16.

If the times are different as in this case then I would remove the later time.

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)
  }
}

3 Answers 3

3

Try this:

$result = array();
foreach ($array as $place) {
    if (!array_key_exists($place['time'], $result)) {
        $result[$place['time']] = $place;
    }
}
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);
1
  • Thanks this worked, I just had to change the $tmp[$city][$amount]['time'] < $row['time'] to $tmp[$city][$amount]['time'] > $row['time'] so that it kept the earlier time and the the later.
    – Ben Paton
    Apr 14, 2014 at 18:25
1

Create a 2-dimensional associative array, where one dimension is keyed off the city, and the other is the amount:

$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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.