Hi, I'm having trouble to remove duplicate entries from multidimensional array. Here is my code: <?php var_dump($entries['demo'] ); ?> array(6) { [0]=> array(4) { ["city"]=> string(8) "Brisbane" ["course"]=> string(19) "Excel 2010 Beginner" ["price"]=> string(3) "330" ["dates"]=> array(2) { [0]=> string(8) "Jul 21st" [1]=> string(8) "Aug 26th" } } [1]=> array(4) { ["city"]=> string(8) "Brisbane" ["course"]=> string(23) "Excel 2010 Intermediate" ["price"]=> string(3) "330" ["dates"]=> array(2) { [0]=> string(8) "Jul 22nd" [1]=> string(8) "Aug 27th" } } [2]=> array(4) { ["city"]=> string(8) "Brisbane" ["course"]=> string(19) "Excel 2010 Advanced" ["price"]=> string(3) "530" ["dates"]=> array(2) { [0]=> string(8) "Jul 23rd" [1]=> string(8) "Aug 28th" } } [3]=> array(4) { ["city"]=> string(6) "Sydney" ["course"]=> string(19) "Excel 2010 Beginner" ["price"]=> string(3) "330" ["dates"]=> array(2) { [0]=> string(8) "Jul 28th" [2]=> string(8) "Aug 26th" } } [4]=> array(4) { ["city"]=> string(6) "Sydney" ["course"]=> string(23) "Excel 2010 Intermediate" ["price"]=> string(3) "330" ["dates"]=> array(2) { [2]=> string(8) "Aug 27th" [0]=> string(8) "Jul 29th" } } [5]=> array(4) { ["city"]=> string(6) "Sydney" ["course"]=> string(19) "Excel 2010 Advanced" ["price"]=> string(3) "330" ["dates"]=> array(2) { [2]=> string(8) "Aug 28th" [0]=> string(8) "Jul 30th" } } } Code (markup): What I'm trying to get is: Brisbane Sydney My code: <?php $entries = get_option('form_options'); $count = 0; foreach ( (array) $entries['demo'] as $key => $entry ) { $count++; $city = ''; if ( isset( $entry['city'] ) ) $city = $entry['city']; ?> <option class="<?php echo $city; ?>" value="<?php echo $city; ?>"><?php echo $city; ?></option> <?php } ?> Code (markup): With this code I get next: Brisbane Brisbane Brisbane Sydney Sydney Sydney
You can hold the displayed city in a separate array. See code below: <?php $entries = get_option('form_options'); $count = 0; $displayed_city = array(); foreach((array) $entries['demo'] as $key => $entry){ $count++; $city = ''; if( isset($entry['city'])) $city = $entry['city']; if(!in_array($city, $displayed_city)){ $displayed_city[] = $city; ?> <option class="<?php echo $city; ?>" value="<?php echo $city; ?>"><?php echo $city; ?></option> <?php } } ?> PHP:
Filter it as so: $entries = get_option('form_options'); $unique_list = array_intersect_key( $entries['demo'], array_unique(array_map(function($item) { return $item['city']; }, $entries['demo'])) ); Code (markup): The resulting $unique_list would only contain the unique cities with no duplicates.
comparing how @krishmk and @ThePHPMaster approached it reminds me of something I'm always pointing out about PHP, particularly to people coming from other languages. "Yeah, there's a existing function for that." Quite often you see programmers resorting to the brute force approach when PHP is already able to do that for you. though guys, you do know that there is NO reason to EVER have a value identical to the content of an OPTION tag other than to intentionally cause code bloat, right? Much less it's pretty pointless to put classes on OPTION since technically OPTION are NOT supposed to receive style either? (even if FF does let you do so, little else does!) Also @ThePHPMaster, why the array intersect? Seems unnecessary. $entries = get_option('form_options'); echo ' <label for="chooseCity">Choose City:</label> <select name="city" id="chooseCity"> <option>', implode( "</option>\n\t\t<option>", array_unique(array_map(function($item) { return $item['city']; }, $entries['demo'])) ), '</option> </select>'; Code (markup): Is how I'd be going about it.
It is not necessary. I just left it there incase the OP wanted to keep the array structure the same, but if the names are the only thing needed your solution would be better.
Thing is array_unique returns the indexes of the first encountered.... If you had: $test = [ 'alpha' => 1, 'bingo' => 2, 'charlie' => 1 ]; an array_unique of that would return: ['alpha' => 1, 'bingo' => 2] As it DOES preserve indexes, it just removes elements whose values already exist. See the examples: http://php.net/manual/en/function.array-unique.php#refsect1-function.array-unique-examples Indexes are preserved for the first occurances, only the redundant values and their indexes are removed... which is actually pretty cool... More useful (not for this scenario, just talking in general) would be if one could put them into an array based on the value as a new key... something like: $filtered = []; foreach ($entries['demo'] as $key => $item) { if (!isset($filtered[$item['city']])) { $filtered[$item['city']] = [ $key ]; } else $filtered[$item['city']][] = $key; } Code (markup): Gives you unique city as the key, and the keys as a array indexing the original array if need be. I had thought PHP had a function to do that already, but it's my turn to forget what that function is.