I am using the following code:
foreach ($record as $doc)
{
$groupIds[] = $doc['groupId'];
}
$gpids = "'".implode("','",array_unique($groupIds))."'";
$collection5 = $db->chat;
$cursor = $collection5->find(array('groupId' => array('$in' => array($gpids))));
foreach($cursor as $res)
{
print_r($res);
}
but no results will come. Please help me.
That is because your $gpids is a string and you end up putting one element array in the $in query. This should work:
$collection5->find(array('groupId' => array('$in' => array_unique($groupIds))));
Related
I have written following code in php and am using mongo db as database
$projection = array("broadcast_id" => 1,"studentList" => 1);
$query = array('broadcast_id'=> $broadcast_id);
$count = $this->collection->find($query)->count();
$cursor = $this->collection->find($query,$projection);
$result = array();
foreach($cursor as $row)
{
$idstring = trim($row["studentList"]);
$idstring = preg_replace('/\.$/', '', $idstring);
$idarray = explode('|', $idstring);
foreach($idarray as $studentId)
{
$this->StudentCollection = $this->db->studentTbl;
$StudentCursor= $this->StudentCollection->find(array("student_id" => $studentId));
if($StudentCursor->count() > 0)
{
foreach ($StudentCursor as $k => $srow) {
array_push($result, $srow);
}
}
else
{
array_push($result, array("datanotfound"=>1));
}
}
}
return json_encode($result);
After fetching "studentList" from broadcastTbl table and $idstring has "5042|5043|5044" values which are student ids of studentTbl. Now I am trying to fetch corresponding students details by splitting them one by one on the basis of "|". After that I am trying to push them in array $result.
It always displays $StudentCursor->count() as "1" and never enter in else block even if find() query fails to find record and then it displays output as [] i.e it always stay in if statement !!!
Please help me in tracking out what is wrong in the code and writting it efficiently!!!
I am trying to add data to an array $result where each value is a string $idstring which will contain the employees ids like "999|888|777". The data is fetched from broadcastTbl collection from a MongoDB. I need to fetch each employee's corresponding details in a loop and push to $result. I am successfully fetching the data, but the trouble I am facing is in the below code where only one employees' data is getting pushed to the $result array.
$projection = array("broadcast_id" => 1, "employeeList" => 1);
$query = array("broadcast_id" => $broadcast_id);
$count = $this->collection->find($query)->count();
$cursor = $this->collection->find($query, $projection);
$result = array();
foreach($cursor as $row)
{
$idstring = trim($row["employeeList"]);
$idstring = preg_replace('/\.$/', '', $idstring);
$idarray = explode('|', $idstring);
foreach($idarray as $employeeId)
{
$this->EmployeeCollection = $this->db->EmployeesTbl;
$EmployeeCursor= $this->EmployeeCollection->find(array("EmployeeNumber" => $employeeId));
$EmployeeCursorCount= $this->EmployeeCollection->find(array("EmployeeNumber" => $employeeId))->count();
if($EmployeeCursor->count() > 0)
{
array_push($result,$EmployeeCursorCount);
foreach ($EmployeeCursor as $k => $row) {
array_push($result, $row);
}
}
else
{
array_push($result, array("datanotfound"=>1));
}
return json_encode($result);
}
}
You are returning too early.
Move return json_encode($result);
After the outer-most foreach loop.
I try use this solution for pagination in PHP:
public function getRecords($page, $count, $currentId)
{
$query = ["_id" => ['$gt' => $currentId]]; //what's wrong here?
$cursor = $this->recordsCollection->find($query)->
skip(($page-1) * $count)->
limit($count)->
sort(["_id" => true]);
$result = array();
foreach ($cursor as $doc) {
array_push($result, $doc);
}
return $result;
}
But it returns an empty array result. Also I tried this query without skip and limit, but the result still was an empty array. If $query is empty, everything is ok, it returns all records in colection.
What I'm doing wrong?
SOLUTION$query = ["_id" => ['$gt' => new MongoId($currentId)]];
Maybe $currentId is of wrong type? Looking at what you're trying to do I think it should be instance of MongoId
Simple question to which I don't have an answer.
How can I change my array from this:
[{"sku":"6"},{"buyers":"7"},{"base":"8"}]
to this:
[{"sku":"6","buyers":"7","base":"8"}]
I have three queries for three different database tables:
$sku = DB::table('mapiranje')->select(DB::raw('count(*) as sku'))
->where('mate_fk', '=', NULL)
->get();
$kupac = DB::table('mapkupci')->select(DB::raw('count(*) as buyers'))
->where('kupci_fk', '=', NULL)
->get();
$base = DB::table('dist_base')->select(DB::raw('count(*) as base'))
->where('base_fk', '=', NULL)
->get();
now each returns:
[{"sku":"6"}]
[{"buyers":"6"}]
[{"base":"6"}]
I have used merge_array to make a single array, but I get:
[{"sku":"6"},{"buyers":"7"},{"base":"8"}]
what I want is:
[{"sku":"6","buyers":"7", "base":"8"}]
Refactor your code according to right Laravel way:
$result = [
'sku' => DB::table('mapiranje')->whereNull('mate_fk')->count(),
'buyers' => DB::table('mapkupci')->whereNull('kupci_fk')->count(),
'base' => DB::table('dist_base')->whereNull('base_fk')->count()
];
$result = [];
foreach($input as $oneInputRow) {
$result[$oneInputRow[0]] = $oneInputRow[1];
}
$target = array();
$start = array(array("sku"=>"6"), "buyers"=>"7"), "base"=>"8"));
foreach($start as $sub){
foreach($sub as $key => $val){
$target[$key] = $val;
}
}
Not shure if laravel provides any special syntax, but just with php I'd do it as above.
Basicly you loop over the start-array. In that you loop over every array to get the key/val combination and put that into the target-array.
For the second loop there would be other ways if you only have one entry in every secondary array.
Please try below code
$dd = '[{"sku":"6"},{"buyers":"7"},{"base":"8"}]';
$data = json_decode($dd,true);
$result = array();
foreach($data as $key=>$value){
foreach($value as $key1=>$value1){
$result[$key1] = $value1;
}
}
echo json_encode($result); //this will print your required format result
I am having a bit of trouble trying to explain this correctly, so please bear with me...
I need to be able to recursively select keys based on a given array. I can do this via a fairly simple foreach statement (as shown below). However, I prefer to do things via PHP's built in functions whenever possible.
$selectors = array('plants', 'fruits', 'apple');
$list = array(
'plants' => array(
'fruits' => array(
'apple' => 'sweet',
'orange' => 'sweet',
'pear' => 'tart'
)
)
);
$select = $list;
foreach ($selectors as $selector) {
if (isset($select[$selector])) {
$select = $select[$selector];
} else {
exit("Error: '$selector' not found");
}
}
echo $select;
See this code in action
My Question: Is there a PHP function to recursively select array keys? If there is not, is there a better way than in the example above?
If i understand , you are searching about :
http://php.net/recursivearrayiterator
and
http://php.net/recursiveiteratoriterator
And code something like that:
$my_itera = new RecursiveIteratorIterator(new RecursiveArrayIterator($my_array));
$my_keys = array();
foreach ($my_itera as $my_key => $value) {
for ($i = $my_itera->getDepth() - 1; $i >= 0; $i--) {
$my_key = $my_itera->getSubIterator($i)->key() . '_' . $my_key;
}
$my_keys[] = $my_key;
}
var_export($my_keys);
I Hope it works.