Last active
September 27, 2017 03:15
-
-
Save phucdohong96/2798d561709e34c943e89d52baf4acd5 to your computer and use it in GitHub Desktop.
Check duplicate values in PHP array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /*Check duplicate values in PHP array*/ | |
| $country_array = []; | |
| $args = array( | |
| 'post_type' => 'distributor', | |
| 'posts_per_page' => '-1' | |
| ); | |
| $loop = new WP_Query($args); | |
| while($loop->have_posts()): $loop->the_post(); | |
| $id = get_the_ID(); | |
| $title = get_the_title(); | |
| $country_filter = get_field('country_filter'); | |
| if (!empty($country_filter)) { | |
| foreach ($country_filter as $key => $value_filter) { | |
| array_push($country_array, $value_filter); | |
| } | |
| } | |
| endwhile; | |
| wp_reset_query(); | |
| $country_array = array_unique($country_array); | |
| foreach ($country_array as $key => $label) { | |
| $value = sanitize_title($label); | |
| echo '<pre>'; | |
| var_dump($label); | |
| echo '</pre>'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /*Check duplicate values and count in PHP array*/ | |
| $country_array = []; | |
| $args = array( | |
| 'post_type' => 'distributor', | |
| 'posts_per_page' => '-1' | |
| ); | |
| $loop = new WP_Query($args); | |
| while($loop->have_posts()): $loop->the_post(); | |
| $id = get_the_ID(); | |
| $title = get_the_title(); | |
| $country_filter = get_field('country_filter'); | |
| if (!empty($country_filter)) { | |
| foreach ($country_filter as $key => $value_filter) { | |
| array_push($country_array, $value_filter); | |
| } | |
| } | |
| endwhile; | |
| wp_reset_query(); | |
| $array_count_values = array_count_values($country_array); | |
| foreach ($array_count_values as $label => $key) { | |
| $value = sanitize_title($label); | |
| echo '<pre>'; | |
| var_dump($label); | |
| echo '</pre>'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment