Quantcast
Channel: phpHelp.co » PHP
Viewing all articles
Browse latest Browse all 15

PHP MongoDB case insensitive sorting (Solved)

$
0
0

Although MondoDB does not offer it, but you can very easily do it in PHP or any other language. Here is how to do it in PHP.

<?php

$cn = new MongoClient($dbHost);
$db = $cn->selectDB($dbName);
$col = new MongoCollection($db, $collectionName);

$cursor = $col->find();
$cursor = iterator_to_array($cursor);

foreach ($cursor as $key => $row) {
$name[$key] = $row['name'];
$email[$key] = $row['email'];
}
//$name is the field to sort on, taken from the above loop
//You can use SORT_ASC or SORT_DESC

array_multisort($name, SORT_ASC, $cursor);

foreach ($cursor as $doc) {
echo $doc['name'].’-’.$doc['email'].’<br/>’;
}

?>

download


Viewing all articles
Browse latest Browse all 15

Latest Images

Trending Articles





Latest Images