从不同的数组php删除重复值

时间:2014-04-24 11:07:19

标签: php arrays

我有一个问题是从三个不同的数组中删除重复条目。让我创建一个数组的结构。

 array (
'table_id' => 
array (
  1 => '1',
  2 => '1',
  3 => '1',
  4 => '1', 
  5 => '1',
  6 => '1',
  7 => '1',
  8 => '1', <-- This entry have value '1' : 
  14 => '1',
  17 => '1',
  20 => '1', <-- This entry have value '1' : 
  21 => '1',
  25 => '1',
  28 => '1', <-- This entry have value '1' : 
),
'no_of_seats' => 
array (
  1 => '2',
  2 => '2',
  3 => '2',
  4 => '2',
  5 => '2',
  6 => '4',
  7 => '4',
  8 => '4', <-- This entry have value '4' : 
  14 => '8',
  17 => '2',
  20 => '4', <-- This entry have value '4' : 
  21 => '8',
  25 => '6',
  28 => '4', <-- This entry have value '4' : 
),
'dt_time_slot' => 
array (
  1 => '4:30',
  2 => '6:30',
  3 => '9:00',
  4 => '17:00',
  5 => '23:00',
  6 => '0:30',
  7 => '1:30',
  8 => '4:30', <-- This entry have value '4:30' : 
  14 => '4:00',
  17 => '19:00',
  20 => '4:30', <-- This entry have value '4:30' : 
  21 => '6:30',
  25 => '7:30',
  28 => '4:30', <-- This entry have value '4:30' : 
));

结果数组应为:

 array (
'table_id' => 
array (
  1 => '1',
  2 => '1',
  3 => '1',
  4 => '1', 
  5 => '1',
  6 => '1',
  7 => '1',
  8 => '1', <-- This entry have value '1' : 
  14 => '1',
  17 => '1',
  21 => '1',
  25 => '1',
),
'no_of_seats' => 
array (
  1 => '2',
  2 => '2',
  3 => '2',
  4 => '2',
  5 => '2',
  6 => '4',
  7 => '4',
  8 => '4', <-- This entry have value '4' : 
  14 => '8',
  17 => '2',
  21 => '8',
  25 => '6',

),
'dt_time_slot' => 
array (
  1 => '4:30',
  2 => '6:30',
  3 => '9:00',
  4 => '17:00',
  5 => '23:00',
  6 => '0:30',
  7 => '1:30',
  8 => '4:30', <-- This entry have value '4:30' : 
  14 => '4:00',
  17 => '19:00',
  21 => '6:30',
  25 => '7:30',

));

什么是真实重复意味着所有三个不同的数组都具有相同的键,如1,2,3,...... 14,.... 25。我希望三个数组中的相同键不应包含相同的值...如果它们包含相同的值,那么我将其标记为重复...

我想要的是从三个不同的数组中删除重复的条目(重复的条目可以大于3.这次是它的3.)并且只保留一个值。

请让我知道我该怎么做。

1 个答案:

答案 0 :(得分:1)

您可以使用array_unique http://www.php.net/manual/en/function.array-unique.php

   $array['table_id'] = array_unique($array['table_id']);
   $array['no_of_seats'] = array_unique($array['no_of_seats']);
   ...etc