简体   繁体   English

php在特定数组列中有多少个唯一值

[英]php how many unique values in specific array column

I'm getting the desired results back from my query and now I'm having trouble figuring out how to manipulate the data into something usable. 我从查询中得到了想要的结果,现在我很难弄清楚如何将数据处理成可用的东西。

The ultimate goal is a json output to be used by google charts and I can do this on any one single component without a problem. 最终目标是要由Google图表使用的json输出,我可以在任何单个组件上完成此操作,而不会出现问题。

I need the output to look like this (I have no idea how many components will come after Month/Year... it might be 0 or 10 or 20...): 我需要输出看起来像这样(我不知道在月/年之后会有多少个分量……可能是0或10或20 ...):

      ['Month/Year', 'Wiper Blades', 'Mufflers'] [,[...]],
      ['March 2013',  13,      11],
      ['April 2013',  17,      19],
      [...],
      [...]
      ]);

Here is the array I have after my query ( can't do this in a while() because I need the cmpnt_name for the first line of the desired output (above) which is why I'm building the array first): 这是查询后我拥有的数组(不能在while()中执行此操作,因为我需要cmpnt_name作为所需输出的第一行(上面),这就是我首先构建数组的原因):

Array
(
  [0] => Array
      (
          [vmonth] => February
          [vyear] => 2013
          [cmpnt_count] => 9
          [cmpnt_name] => Wiper blade
      )

  [1] => Array
      (
          [vmonth] => March
          [vyear] => 2013
          [cmpnt_count] => 13
          [cmpnt_name] => Wiper blade
      )

  [2] => Array
      (
          [vmonth] => March
          [vyear] => 2013
          [cmpnt_count] => 11
          [cmpnt_name] => Muffler
      )

  [3] => Array
      (
          [vmonth] => April
          [vyear] => 2013
          [cmpnt_count] => 17
          [cmpnt_name] => Wiper blade
      )

  [4] => Array
      (
          [vmonth] => April
          [vyear] => 2013
          [cmpnt_count] => 19
          [cmpnt_name] => Muffler
      )
)

I can't loop through the array and take every cmpnt_name and append that to the first line of the desired output... I only want uniques. 我无法遍历数组并获取每个cmpnt_name并将其附加到所需输出的第一行...我只想要唯一性。

Also a component may not have any views for a given month (it may not have even existed that month) so I may have components that date from Feb and other components that don't get added until later but are included in the count... ie Muffler was added in March. 另外,某个组件在给定月份中可能没有任何视图(该月份可能甚至不存在),因此我可能有一些组件的日期是从2月开始,而其他组件直到后来才添加,但都包含在计数中。 。例如,消声器在三月添加。

So how do I go about finding the unique component names for the first row? 那么,如何找到第一行的唯一组件名称呢?

I was thinking: 我刚在想:

function findUniques($cmpnt)
{
  $names = array();
  foreach($cmpnt as $item)
  {
    $key = $item['cmpnt_name'];
    if(array_key_exists($key, $names))
    {
      $names[$key]++;
    }
    else
      $names[$key] = 1;
  }
  return($names);
}

Is there a php function for this? 有没有为此的PHP功能?

Is there a better way to do this? 有一个更好的方法吗?

您应该能够执行array_column,然后执行唯一的数组,因此请尝试以下操作:

array_unique(array_column($cmpnt, 'cmpnt_name'));

I wonder if you would have some luck with array_map which allows you to extract all elements with a particular key. 我想知道您是否对array_map ,它可以让您使用特定键提取所有元素。 Example

Function compKey($v) {
    Return $v['cmpnt_name'];
}
CKeys = array_map("compKey", $cmpnt);
UniqueComponents = array_unique(CKeys);

Not sure syntax exactly right as typing this on iPhone, and don't know if more efficient. 在iPhone上键入语法时,不确定语法是否正确,也不知道是否更有效。 But worth a try? 但是值得一试吗?

Is there a php function for this? 有没有为此的PHP功能?

No. array_unique() is the closest, but does not handle dimensional arrays. array_unique()是最接近的数组,但不处理维数组。

Is there a better way to do this? 有一个更好的方法吗?

Unfortunately, PHP (currently) does not have array functions that pluck keys into an array so you could then use array_unique() . 不幸的是,PHP(目前)不具有钥匙到一个数组的数组函数,所以你可以再使用array_unique()

As such, short of some micro-optimizations or refactoring using array functions, what you have is generally what you have to do. 这样,除了使用数组函数进行一些微优化或重构外,您通常所要做的就是。

However, you do have typo: 但是,您确实有错别字:

$names[$key] = ++$count;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM