Reputation: 985
How to fetch and display only the distinct values of a key for all elements using PHP. In my application I am already fetching data using the below code and assigning that data to an array which will be further used in JS.
<?php
require 'C:\ProgramData\ComposerSetup\bin\vendor\autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->test->bf;
$clubSelectList = array();
$clubSelectListResult = $collection->find();
foreach ($clubSelectListResult as $option) {
echo $option['club'], "<br><br>";
}
?>
How can I fetch unique values. Any help is appreciated. Thanks.
Update: This does it.
$clubSelectListResult = $collection->distinct('club');
foreach ($clubSelectListResult as $option) {
echo $option, "<br><br>";
}
Upvotes: 1
Views: 82
Reputation: 17091
You have to call distinct
method:
$collection->distinct('your_distinct_field');
Upvotes: 3