php - get only unique values from array into dropdown list -


i have array of data similar following:

$array[0] = array('key1' => 'value1a','key2' => 'value2a','key3' => 'value3a'); $array[1] = array('key1' => 'value1a','key2' => 'value2b','key3' => 'value3b'); $array[2] = array('key1' => 'value1b','key2' => 'value2b','key3' => 'value3c'); $array[3] = array('key1' => 'value1c','key2' => 'value2c','key3' => 'value3c'); 

i need use data create set of dropdown lists based on each key. however, want remove duplicates, each dropdown list shows unique values.

i started with:

<select> <?php        foreach($array $arr)         {             print "<option>".$arr['key1']."</option>";         }; ?> </select></br> 

as expected, gave me 4 entries, 2 of same.

so tried:

<select> <?php        foreach(array_unique($array) $arr)         {             print "<option>".$arr['key1']."</option>";         }; ?> </select></br> 

and also:

<select>     <?php            $arr = array_unique($array);         foreach ($arr $a)             {                 print "<option>".$a['key1']."</option>";             };     ?> </select></br>   

but these give me 1 item (value1a). on closer inspection see has generated string of errors:

notice:  array string conversion in c:\apache24\htdocs\test\test29.php on line 39 

but can't figure out why is, or how fix list want?

how can list of unique entries?

since php 5.5, can use array_column function:

    foreach(array_unique(array_column($array, 'key1')) $arr)     {         print "<option>".$arr."</option>";     }; 

but, have older version, can do:

    foreach(array_unique(array_map(function($rgitem)     {        return $rgitem['key1'];     }, $array)) $arr)     {         print "<option>".$arr."</option>";     }; 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -