php 特定数组列中有多少个唯一值 [英] php how many unique values in specific array column

查看:35
本文介绍了php 特定数组列中有多少个唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的查询中得到了想要的结果,但现在我无法弄清楚了解如何将数据处理成可用的东西.

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.

最终目标是 google 图表使用的 json 输出,我可以在任何一个组件都没有问题.

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.

我需要输出看起来像这样(我不知道后面会出现多少个组件月/年……可能是 0 或 10 或 20 ……):

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...):

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

这是我查询后的数组(暂时无法执行此操作(),因为我需要所需输出的第一行的 cmpnt_name(上图),这就是我首先构建数组的原因):

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

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

我无法遍历数组并获取每个 cmpnt_name 并将其附加到所需输出的第一行...我只想要唯一的.

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.

另外一个组件可能在给定的月份没有任何视图(它甚至可能没有那个月存在)所以我可能有从 2 月开始的组件和其他组件直到稍后才添加但包含在计数中......即添加了消声器三月.

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... i.e. Muffler was added in March.

那么如何找到第一行的唯一组件名称?

So how do I go about finding the unique component names for the first row?

我在想:

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

是否有针对此的 php 函数?

Is there a php function for this?

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

你应该可以做array_column,然后array unique,所以试试这个:

You should be able to do array_column, and then array unique, so try this:

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

这篇关于php 特定数组列中有多少个唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆