0

我有一个包含“title”和“url”(两个字符串)的二维数组,如下所示。是否可以检查“url”中的重复项,如果存在,也删除相应的“标题”?

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

3 回答 3

1

试试这个功能它可以完成这项工作

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');
于 2012-07-10T11:44:40.067 回答
1

或者,如果您对使用 url 的最后一个值感到满意,您可以

foreach($array as $subarray) {
    $output[$subarray['url']] = $subarray['title'];
}
于 2012-07-10T11:46:30.683 回答
0

尝试这样的事情:

$originals = array();

foreach($array as $key => $value) {
   if(!isset($originals[$value['url']])) {
      $originals[$value['url']] = true;
   }
   else {
      // exists already, delete entry
      unset($array[$key]);
   }
}
于 2012-07-10T11:45:26.573 回答