{"id":564,"date":"2019-08-22T17:30:21","date_gmt":"2019-08-23T00:30:21","guid":{"rendered":"http:\/\/jasonflaherty.com\/blog\/?p=564"},"modified":"2023-10-17T21:21:55","modified_gmt":"2023-10-18T04:21:55","slug":"create-a-faceted-sort-with-php-using-array_count_valuesarray","status":"publish","type":"post","link":"https:\/\/jasonflaherty.com\/blog\/2019\/08\/22\/create-a-faceted-sort-with-php-using-array_count_valuesarray\/","title":{"rendered":"Create a Faceted Sort with PHP using array_count_values($array)"},"content":{"rendered":"\n<p>I was working on a project where there was about 200 records. I needed a simple way to show the categories each record belonged to in order for the user to narrow down the results based on the category they selected. This is generally known as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Faceted_search\">Faceted Search<\/a>. <\/p>\n\n\n\n<p>PHP has a nice built in <a href=\"https:\/\/www.php.net\/manual\/en\/function.array-count-values.php\">function<\/a> for us with array_count_values($array). This returns an array of your array containing two dimensions: Key and Count. The way I ended up using it was simple for my project. Here is a <a href=\"https:\/\/gist.github.com\/jasonflaherty\/112eaa434bea0af960448a4b026fe9f9\">simple gist demonstration<\/a> hard coded as we are using a MongoDB to store the data in our project&#8230; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a project where there was about 200 records. I needed a simple way to show the categories each record belonged to in order for the user to narrow down the results based on the category they selected. This is generally known as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Faceted_search\">Faceted Search<\/a>. <\/p>\n<p class=\"excerpt-link\"><a href=\"https:\/\/jasonflaherty.com\/blog\/2019\/08\/22\/create-a-faceted-sort-with-php-using-array_count_valuesarray\/\">&sim;&nbsp;Continue Reading&nbsp;&sim;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[12,158,1],"tags":[160,161,159],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5OfD9-96","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts\/564"}],"collection":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/comments?post=564"}],"version-history":[{"count":1,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts\/564\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/media?parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/categories?post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/tags?post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}