0

I am using laravel, I am getting this error when trying to count the valuse of this array.

Error:array_count_values(): Can only count STRING and INTEGER values!

the function:

public function test()
{
  $test = \DB::table('surveys')->where('company_id', '=', 1)->select('rd1')->get()->toArray();;
  $c = array_count_values($test);
  $val = array_search(max($c), $c);
  return view ('companies.test', compact('val'));

}

here is a var_dump of $test:

  array:4 [▼
  0 => {#204 ▼
    +"rd1": "option1"
  }
  1 => {#206 ▼
    +"rd1": "option1"
  }
  2 => {#207 ▼
    +"rd1": "option1"
  }
  3 => {#208 ▼
    +"rd1": "option1"
  }
]
11
  • 1
    what is the output of var_dump($test) ?
    – georoot
    Commented Dec 8, 2016 at 21:43
  • Looks like you want count, not array_count_values. array_count_values will loop over an array and key a count of how many times a value exists in an array. So an array like [1, 2, 3, 1] would return [1=>2, 2=>1, 3=>1] meaning that value 1 exists 2 times in the array..etc. count will return just the number of rows in the array, which with the example is 4. Commented Dec 8, 2016 at 21:47
  • it says it all, if the elements of the array aren't string or numbers you can't count their frequency using this function.
    – sidyll
    Commented Dec 8, 2016 at 21:47
  • 2
    You should be able to do this in your query with group by and count. Commented Dec 8, 2016 at 21:59
  • 1
    Have a look at this question for a better method like @Don'tPanic is suggesting: stackoverflow.com/questions/18533080/… Commented Dec 8, 2016 at 22:03

2 Answers 2

0

Your array is 2d which array_count_values doesn't like. You can either perform a loop and count the values yourself, or use something like array_column to get an array of the value from just the one column. Example:

array_count_values(array_column($test, $columnName))

array_column will get an array of values from just the one column you specify, which in your case is a string and that returned array can be passed to array_count_values to get the result you want.

Of course though, as someone else commented, this grouping and counting can be done by the database much more efficiently (you would be passing back less data and the database can be optimized to return queries like this with indexes). Here is another question that covers this.

You could then turn that back into a similar array with just array_column like:

\DB::table('surveys')
    ->where('company_id', '=', 1)
    ->select('rd1', \DB::raw('count(*) as total')
    ->groupBy('rd1')
    ->get()
    ->toArray();;
array_column($test, 'total', 'rd1');
0

The built-in function array_count_values() can't calculate the frequency if the elements are not strings or numbers. You can, however, easily perform a loop to do so:

foreach ($array as $e)
    $frequency[$e] =+ 1;

This would basically do the same as the built-in. Note that the elements are used as keys for the final frequency array (and you can imagina that if $e is not a valid key (string or number) it will fail). In your case, you need to use as key a property of your element. For example, if it is another array:

foreach ($test as $e)
    $frequency[$e['survey_name']] += 1;

Or properties/methods of an object:

foreach ($test as $e)
    $frequency[$e->myColumn()] += 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.