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"
}
]
var_dump($test)
?count
, notarray_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 value1
exists2
times in the array..etc.count
will return just the number of rows in the array, which with the example is4
.