ppetree
ppetree

Reputation: 816

Generate array of 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.

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],
      [...],
      [...]
      ]);

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

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.

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?

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?

Is there a better way to do this?

Upvotes: 0

Views: 1935

Answers (3)

progwhiz
progwhiz

Reputation: 48

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

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

Upvotes: 2

Floris
Floris

Reputation: 46365

I wonder if you would have some luck with array_map which allows you to extract all elements with a particular key. 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. But worth a try?

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 72971

Is there a php function for this?

No. array_unique() is the closest, but does not handle dimensional arrays.

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

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;

Upvotes: 0

Related Questions