I have an 2d array that contains a 'title' and 'url' (both strings) as shown below. Is it possible to check for duplicates in the 'url' and if they exist, delete the corresponding 'title' too?
array
0 =>
array
'title' => string 'China - Wikipedia, the free encyclopedia'
'url' => string 'http://en.wikipedia.org/wiki/China'
1 =>
array
'title' => string 'China'
'url' => string 'http://www.state.gov/r/pa/ei/bgn/18902.htm'
2 =>
array
'title' => string 'China | World news | The Guardian'
'url' => string 'http://www.guardian.co.uk/world/china'
3 =>
array
'title' => string 'China Travel Information and Travel Guide - Lonely Planet'
'url' => string 'http://www.lonelyplanet.com/china'
4 =>
array
'title' => string 'ChinaToday.com'
'url' => string 'http://www.chinatoday.com/'
Try this function it could do the job
function super_unique($array,$key)
{
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$yourearray = super_unique($arr,'url');
Try something like this:
$originals = array();
foreach($array as $key => $value) {
if(!isset($originals[$value['url']])) {
$originals[$value['url']] = true;
}
else {
// exists already, delete entry
unset($array[$key]);
}
}
Or if you are happy with using the last value of the url you could
foreach($array as $subarray) {
$output[$subarray['url']] = $subarray['title'];
}