Hi I have a mongodb database where i store products and each product doc is like below :
Using php I want to print all instances of "user"
from the "comments"
array
What I tried :
$collection=$db->products;
$itemID = $_POST['itemID'];
$cursor = $collection->findOne(["id"=>$itemID]); //find an item
//print all user names who commented on item
foreach($cursor['comments'] as $c){
echo "<p> User : ".$c["user"]." </p>";
}
";
exit();
And I get :
Warning: Invalid argument supplied for foreach()
This is the first time I do this so I would appreciate your help
question from:https://stackoverflow.com/questions/65861529/print-mongodb-array-field-values-of-document-using-php