0

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
ppetree
  • 826
  • 3
  • 15
  • 31

3 Answers3

2

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

array_unique(array_column($cmpnt, 'cmpnt_name'));
Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
progwhiz
  • 48
  • 5
  • 1
    whoever downvoted, can you please explain why? I found this to work well for me, and was less complicated than the other answer below IMHO.. – progwhiz Dec 22 '14 at 10:52
  • Note that `array_column` is new in PHP 5.5. – Shai Oct 06 '15 at 14:44
1

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?

Floris
  • 45,857
  • 6
  • 70
  • 122
  • + However, I consider this equivalent to the original code *refactored using array functions*. The simple answer is PHP currently can not do what is asked with a single function. – Jason McCreary Apr 26 '13 at 12:11
  • @jasonmccreary - I don't disagree. I thought it was a cool use of `array_map` though. I suspect there is no perceptible performance difference. – Floris Apr 26 '13 at 12:27
  • to me, it's the same issue... extract all the elements with the same key and you get all the elements. – ppetree Apr 26 '13 at 14:34
0

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;
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • see my answer for the closest thing I could figure out for "pluck" function. Haven't figured out how to pass name of elect as second parameter, so hard coded it for this example - but it works. – Floris Apr 26 '13 at 10:19