PHP-如何根据键的值减少数组

时间:2017-06-22 11:33:41

标签: php arrays

我有一个这样的数组:

Array
(
    [0] => Array
        (
            [CommodityID] => 10
            [RetailerID] => 1798
            [Name] => Petrol
            [City] => Parow
            [Line1] => 49 Fifth Avenue
            [Line2] => 
            [Line3] =>  
            [ContractorName] => BP Fifth Ave Motors Parow
            [AreaCode] => 021
            [Number] => 9305025
            [Latitude] => -33.896493
            [Longitude] => 18.579302
            [LogoUrl] => https://webservices.test.com/ContractorLogos/bp.png
            [dist] => 0.00
        )

    [1] => Array
        (
            [CommodityID] => 10
            [RetailerID] => 1798
            [Name] => Petrol
            [City] => Parow
            [Line1] => 49 Fifth Avenue
            [Line2] => 
            [Line3] =>  
            [ContractorName] => BP Fifth Ave Motors Parow
            [AreaCode] => 021
            [Number] => 9305025
            [Latitude] => -33.896493
            [Longitude] => 18.579302
            [LogoUrl] => https://webservices.test.com/ContractorLogos/bp.png
            [dist] => 0.00
        )

    [2] => Array
        (
            [CommodityID] => 22
            [RetailerID] => 1758
            [Name] => Jewellers
            [City] => Parow
            [Line1] => Shop 4
            [Line2] => Mc Intyre Square
            [Line3] => Mc Intyre Road
            [ContractorName] => Du Plessis Manufacturing Jewellers Parow
            [AreaCode] => 021
            [Number] => 9300788
            [Latitude] => -33.895200
            [Longitude] => 18.586710
            [LogoUrl] => https://webservices.test.com/ContractorLogos/DuPlessisJewellers.png
            [dist] => 0.01
        )
)

我只想拥有唯一的[RetailerID],即:

Array
(
    [0] => Array
        (
            [CommodityID] => 10
            [RetailerID] => 1798
            [Name] => Petrol
            [City] => Parow
            [Line1] => 49 Fifth Avenue
            [Line2] => 
            [Line3] =>  
            [ContractorName] => BP Fifth Ave Motors Parow
            [AreaCode] => 021
            [Number] => 9305025
            [Latitude] => -33.896493
            [Longitude] => 18.579302
            [LogoUrl] => https://webservices.test.com/ContractorLogos/bp.png
            [dist] => 0.00
        )

    [1] => Array
        (
            [CommodityID] => 22
            [RetailerID] => 1758
            [Name] => Jewellers
            [City] => Parow
            [Line1] => Shop 4
            [Line2] => Mc Intyre Square
            [Line3] => Mc Intyre Road
            [ContractorName] => Du Plessis Manufacturing Jewellers Parow
            [AreaCode] => 021
            [Number] => 9300788
            [Latitude] => -33.895200
            [Longitude] => 18.586710
            [LogoUrl] => https://webservices.test.com/ContractorLogos/DuPlessisJewellers.png
            [dist] => 0.01
        )
)      

我尝试过array_unique,但这似乎使它基于所有列都是唯一的。是否有一个php内置函数可以通过查看一个数组键的值来实现这一点?

1 个答案:

答案 0 :(得分:1)

将迭代数组一次并仅保存该字段的唯一值,例如:

$newArray = array();
foreach($array as $item) {
$newArray[$item['RetailerID']] = $item;
}

这将始终只保存同一类型的最后一项。您可以修改逻辑以满足您的需求。 另一个更安全的可能性是保存唯一值并检查它们:

$exists = array();
$newArray = array();
    foreach($array as $item) {
    if(!in_array($item['RetailerID'],$exists)){
       $newArray[$item['RetailerID']] = $item;
       $exists[] = $item['RetailerID'];
     }

    }