在php中将数据推送到数组时出错

时间:2017-04-03 12:25:56

标签: php arrays mongodb-php

我正在尝试将数据添加到数组$ result。我有一个字符串$ idstring,它将包含从broadcastTbl集合中获取的员工ID,如“999 | 888 | 777”。现在我正在尝试在循环中获取他们(员工)的相应详细信息,然后将它们推送到数组$ result中。现在,从下面的代码中,只有一个员工的数据被推送到数组。我使用mongodb作为数据库平台,在php中使用了以下代码。

$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);
    }
}     

请帮帮我!!!

1 个答案:

答案 0 :(得分:4)

你太早回来了。

移动return json_encode($result);

在最外面的foreach循环之后。