Skip to content

Instantly share code, notes, and snippets.

@phucdohong96
Last active September 27, 2017 03:15
Show Gist options
  • Save phucdohong96/2798d561709e34c943e89d52baf4acd5 to your computer and use it in GitHub Desktop.
Save phucdohong96/2798d561709e34c943e89d52baf4acd5 to your computer and use it in GitHub Desktop.
Check duplicate values in PHP array
<?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>';
}
<?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