2017-03-03 131 views
4

我想从list_title中看到按值删除重复项。我知道有几个问题和答案,但他们的解决方案并不适合我。php按值删除多维数组中的重复项

这里是我试过:

$uniqueArray = array_map("unserialize", array_unique(array_map("serialize", $notify))); 

结果:

Array 
(
[0] => Array 
    (
     [list_id] => 86 
     [list_reference] => 130948 
     [list_title] => Offer: apartment 2+kk 
     [list_city] => Prague 
     [list_date] => 2017-03-03 11:20:35 
     [list_status] => 0 
     [list_creator] => Company A 
     [list_price] => 30000 
     [list_furniture] => ["1","0","0"] 
     [list_accommodation] => flat 
    ) 

[1] => Array 
    (
     [list_id] => 87 
     [list_reference] => 130947 
     [list_title] => Offer: apartment 2+kk 
     [list_date] => 2017-03-03 11:20:35 
     [list_status] => 0 
     [list_creator] => Company B 
     [list_price] => 30000 
     [list_furniture] => ["1","0","0"] 
     [list_accommodation] => flat 
    ) 

[2] => Array ... 

预期的结果应该是那些因为标题中的一个:

Array 
(
[0] => Array 
(
    [list_id] => 86 
    [list_reference] => 130948 
    [list_title] => Offer: apartment 2+kk 
    [list_city] => Prague 
    [list_date] => 2017-03-03 11:20:35 
    [list_status] => 0 
    [list_creator] => Company A 
    [list_price] => 30000 
    [list_furniture] => ["1","0","0"] 
    [list_accommodation] => flat 
) 
+0

是LIST_ID独特:

我只是喜欢的东西解决了它从MySQL查询?所以你想要删除具有相同list_id的数组? – iyop45

+0

以及如何查看最终结果? – RomanPerekhrest

+0

@RomanPerekhrest我编辑我的问题与预期的结果 – Shina

回答

2

试试这个

<?php 
    function super_unique($array) 
    { 
     $result = array_map("unserialize", array_unique(array_map("serialize", $array))); 

     foreach ($result as $key => $value) 
     { 
     if (is_array($value)) 
     { 
      $result[$key] = super_unique($value); 
     } 
     } 

     return $result; 
    } 
    ?> 
+0

谢谢,但我也试过这个,没有工作。看到我编辑的问题的预期结果 – Shina

+0

这个答案会照顾空间的复杂性和时间复杂性? – Manmohan

+0

不需要关心它。 –

0

只需遍历数组并跟踪重复的字段条目。

只有list_idlist_title将被视为重复数组,但可以添加额外的字段。所有其他键都被忽略。

<?php 

$array = [ 
    [ 
     "list_id" => 86, 
     "list_reference" => 130948, 
     "list_title" => "Offer: apartment 2+kk", 
     "list_city" => "Prague", 
     "list_date" => "2017-03-03 11:20:35", 
     "list_status" => 0, 
     "list_creator" => "Company A", 
     "list_price" => 30000, 
     "list_furniture" => '""1","0","0""', 
     "list_accommodation" => "flat" 
    ], 
    [ 
     "list_id" => 87, 
     "list_reference" => 130947, 
     "list_title" => "Offer: apartment 2+kk", 
     "list_date" => "2017-03-03 11:20:35", 
     "list_status" => 0, 
     "list_creator" => "Company B", 
     "list_price" => 30000, 
     "list_furniture" => '""1","0","0""', 
     "list_accommodation" => "flat" 
    ] 
]; 


$duplicateFields = ["list_id" => array(), "list_title" => array()]; 
foreach ($array as $index => $value) { 
    if(in_array($value['list_id'], $duplicateFields['list_id']) || in_array($value['list_title'], $duplicateFields['list_title'])){ 
     unset($array[$index]); 
    }else{ 
     array_push($duplicateFields['list_id'], $value['list_id']); 
     array_push($duplicateFields['list_title'], $value['list_title']); 
    } 
} 

var_dump($array); 

其中产量:

array(1) { 
    [0]=> 
    array(10) { 
    ["list_id"]=> 
    int(86) 
    ["list_reference"]=> 
    int(130948) 
    ["list_title"]=> 
    string(22) "Offer: apartment 2+kk" 
    ["list_city"]=> 
    string(6) "Prague" 
    ["list_date"]=> 
    string(19) "2017-03-03 11:20:35" 
    ["list_status"]=> 
    int(0) 
    ["list_creator"]=> 
    string(9) "Company A" 
    ["list_price"]=> 
    int(30000) 
    ["list_furniture"]=> 
    string(13) """1","0","0""" 
    ["list_accommodation"]=> 
    string(4) "flat" 
    } 
} 
1

所以基本上你想'list_title'列删除重复。让我们假设我们保持这个标题的第一次出现。然后,您可以使用几个标准功能来实现此目的:

// Reverse array, so the first occurrence kept. 
$data = array_reverse($data); 

$result = array_reverse(// Reverse array to the initial order. 
    array_values(// Get rid of string keys (make array indexed again). 
     array_combine(// Create array taking keys from column and values from the base array. 
      array_column($data, 'list_title'), 
      $data 
     ) 
    ) 
); 

这里是working demo

+0

完美地工作。 –

1

谢谢大家,有时过度思考会影响常识。

GROUP BY list_title ORDER BY list_date