簡體   English   中英

php按值從多維數組中刪除重復項

[英]php remove duplicates from multidimensional array by value

我想從list_title看到,按值刪除重復list_title 我知道有幾個問題和答案,但他們的解決方案對我不起作用。

這是我嘗試過的:

$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
)

所以基本上你想通過'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
        )
    )
);

這是工作演示

更新:

基於@mickmackusa評論,代碼可以簡化為:

$result = array_reverse(array_values(array_column(
    array_reverse($data),
    null,
    'list_title'
)));

有關column_key參數規范的文檔column_key進行描述:

返回完整的數組或對象也可能為NULL(這與index_key一起用於重新索引數組)。

試試這個

<?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;
    }
    ?>

謝謝大家,有時過度思考會影響常識。

我只是用MYSQL查詢解決了它:

 GROUP BY list_title ORDER BY list_date

只需循環遍歷數組並跟蹤重復的字段條目。

對於重復數組,只考慮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"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM