从不同的数组中删除具有相同名称和值的重复键

时间:2017-05-16 16:27:48

标签: php arrays

我有一个来自API的以下8个关联数组,其中包含一个名为“platform”的键,其值为5或21。

select 
    VendorId
  , VendorName
  , [Last Date Received]
  , Days_LastInvoiced = datediff(day,[Last Date Received],getdate())
from (
  select top 1 with ties *
  from (
      select a.vendorid,
          a.DOCDATE as [Last Date Received],
          b.VENDNAME as [Vendor Name] 
      from PM20000 a
      inner join PM00200 b on a.VENDORID = b.VENDORID
      where b.VNDCLSID in ('cb', '6')
      union all
      select a.VENDORID,
          a.DOCDATE as [Last Date Received],
          b.VENDNAME as [Vendor Name] 
      from PM30200 a
      inner join PM00200 b on a.VENDORID = b.VENDORID
      where b.VNDCLSID in ('cb', '6')
    ) u
  order by row_number() over (partition by VendorId order by [Last Date Received] desc)
)
order by [Last Date Received] desc

在我的脚本中,我使用foreach循环在我的网站上显示实际平台而不是平台ID(在本例中为“Wii”和“GameCube”而不是5和21)。但是,由于关键“平台”包括8次(4次,“5”为值,4次为“21”),当我做回声时,我有重复。见下文:

我的代码:

Array ( [0] => Array ( [category] => 0 [platform] => 5 [date] => 1163894400000 [region] => 2 [human] => 2006-Nov-19 [y] => 2006 [m] => 11 ) [1] => Array ( [category] => 0 [platform] => 5 [date] => 1165017600000 [region] => 5 [human] => 2006-Dec-02 [y] => 2006 [m] => 12 ) [2] => Array ( [category] => 0 [platform] => 5 [date] => 1165449600000 [region] => 3 [human] => 2006-Dec-07 [y] => 2006 [m] => 12 ) [3] => Array ( [category] => 0 [platform] => 5 [date] => 1165536000000 [region] => 1 [human] => 2006-Dec-08 [y] => 2006 [m] => 12 ) [4] => Array ( [category] => 0 [platform] => 21 [date] => 1165017600000 [region] => 5 [human] => 2006-Dec-02 [y] => 2006 [m] => 12 ) [5] => Array ( [category] => 0 [platform] => 21 [date] => 1165795200000 [region] => 2 [human] => 2006-Dec-11 [y] => 2006 [m] => 12 ) [6] => Array ( [category] => 0 [platform] => 21 [date] => 1166140800000 [region] => 1 [human] => 2006-Dec-15 [y] => 2006 [m] => 12 ) [7] => Array ( [category] => 0 [platform] => 21 [date] => 1166486400000 [region] => 3 [human] => 2006-Dec-19 [y] => 2006 [m] => 12 ) )

我的输出:

foreach($game_platform_arrays as $key => $innerArray){
    foreach($innerArray as $innerKey => $game_platform){
        if ($innerKey == "platform" and $game_platform == "5"){
            $game_platform = "Wii";
            echo $game_platform;
        }
        if ($innerKey  == "platform" and $game_platform == "21"){
            $game_platform = "Nintendo GameCube";
            echo $game_platform;
        }
    }
}

我不知道如何从输出中删除重复项。我最好的猜测是从数组中删除具有相同名称和值的重复键,但我不知道如何继续。如果有人能指出我正确的方向,那将非常感激。

1 个答案:

答案 0 :(得分:0)

以下是您如何执行此操作的示例:

<?php
$game_platform_arrays = [
0 => [
    'platform' => 5,
    ],
1 => [
    'platform' => 5
],
2 => [
    'platform' => 21
    ],
3 => [
    'platform' => 21
    ]
];
$name = '';
foreach($game_platform_arrays as $key => $innerArray){
   foreach($innerArray as $innerKey => $game_platform){
    if ($innerKey == 'platform' && $game_platform == '5' && $name != 'Wii'){
        echo $name = 'Wii';
    }
    if ($innerKey  == 'platform' && $game_platform == '21' && $name != 'Nintendo GameCube'){
        echo $name = 'Nintendo GameCube';
    }
  }
}
?>