I had something very different and was having issues with an array_map code I had written. I asked for help and someone was nice enough to suggest he code below. However it seems to have a couple of errors and since I didn't write it and am not as familiar with array_walk I could use some help getting it to work.
The issue - instead of providing me the actual counts of each object in the DB, it is counting the types instead. As an example, if Type_2 has a qty of 3, it is displaying as 1 since there is only one column with Type_2.
$bl_type = $wpdb->get_results("SELECT Type FROM mytablenamehere");
$bl_type = [
(object)['Type' => 'Type_0'],
(object)['Type' => 'Type_1'],
(object)['Type' => 'Type_1'],
(object)['Type' => 'Type_1'],
(object)['Type' => 'Type_2'],
];
$types = [
(object)['Type' => 'Type_1'],
(object)['Type' => 'Type_2'],
(object)['Type' => 'Type_3'],
(object)['Type' => 'Type_4'],
];
$counts = array_count_values(array_column($bl_type, 'Type'));
array_walk($types, function($item) use ($counts) {
$item->Frequency = $counts[$item->Type] ?? 0;
});
echo '<table id="count"><tr>';
foreach($types as $item) {
echo '<th>', $item->Type, '</th>';
}
echo '</tr><tr>';
foreach($types as $item) {
echo '<td>', $item->Frequency, '</td>';
}
echo '</tr></table>';
An example output of the array:
array(4) { [0]=> object(stdClass)#2657 (1) { ["Type"]=> string(8) "Type_0" } [1]=> object(stdClass)#2658 (1) { ["Type"]=> string(7) "Type_1" } [2]=> object(stdClass)#2659 (1) { ["Type"]=> string(7) "Type_1" } [3]=> object(stdClass)#2660 (1) { ["Type"]=> string(7) "Type_1" } [4]=> object(stdClass)#2661 (1) { ["Type"]=> string(8) "Type_2" }