shanahobo86 shanahobo86 - 1 month ago 6
PHP Question

php arrays - remove duplication and corresponding value

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/'

Answer

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');
Comments