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>";
}
您必须调用distinct
方法:
$collection->distinct('your_distinct_field');
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.