简体   繁体   中英

Getting Unique values from an array in php

I have the below code to get an array of values. Some of the entries show duplicate values which I want to get rid of. I want to show only unique values. using array_unique doesn't seem to work for me.

<?php 
    $archive_data = get_queried_object();
    $archiveId = $archive_data->term_id;
    $categroy_name =  $archive_data->slug;
    $subseminars_found = euromatech_search_subseminars( $archive_data->slug, '', '');
    $array_subseminar = array(); 

    if(!empty($subseminars_found)){
        foreach ($subseminars_found as $month) {
            foreach ($month as $subseminars) {
                foreach ($subseminars as $subseminar) {
                    $array_subseminar = $subseminar['venue'];
                }
                $unique = array_keys(array_flip($array_subseminar));
                echo $unique;
            }

        }
    }
?>

You are not actually building an array of venues like you think you are. Within your nested foreach loops you are doing:

$array_subseminar = $subseminar['venue'];

Each time it hits that, it overwrites the previous value with a new one. Instead, you should be adding them to the array:

$array_subseminar[] = $subseminar['venue'];

Then, you can get the unique ones from it:

$unique = array_unique($array_subseminar);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM