我正在处理 zend 框架中的一个项目,我正在尝试将我的 SQL 查询的结果合并到一个数组中,因为$result
这是我的查询:
public function getOpen()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('to' => 'track_open'), 'e.email_id = to.email_id', array(''))
->where('e.email_id = to.email_id')
//->where('DATE( open_date ) = ? ', $date )
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getUnsubscribe()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('tu' => 'track_unsubscribe'), 'e.email_id = tu.email_id', array(''))
->where('e.email_id = tu.email_id')
//->where('DATE( unsubscribe_date ) = ? ', $date )
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getClick()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )'),
'tc.click_date',))
->join(array('tc' => 'track_click'), 'e.email_id = tc.email_id', array(''))
->where('e.email_id = tc.email_id')
//->where('DATE( click_date )= ? ', $date )
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getlogs()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('ee' => 'email_errors'),
array( 'isp' => new Zend_Db_Expr('ee.FAI'),
'sent' => new Zend_Db_Expr("SUM( ee.STATUS =0 )"),
'deferred' => new Zend_Db_Expr("SUM( ee.STATUS =1 )"),
'bounced' => new Zend_Db_Expr("SUM( ee.STATUS =2 )")))
->group(new Zend_Db_Expr("FAI"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getSB()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('tb' => 'track_bounce'), 'e.email_id = tb.email_id', array(''))
->where('e.email_id = tb.email_id')
//->where('DATE( bounce_date ) = ? ', $date )
->where('tb.bounce_type = ?', "soft")
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getHB()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('tb' => 'track_bounce'), 'e.email_id = tb.email_id')
->where('e.email_id = tb.email_id')
//->where('DATE( bounce_date ) = ? ', $date )
->where('tb.bounce_type = ?', "hard")
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
这是我为合并所做的功能:
function array_merge_rec($arrays)
{
$result = array();
$keys = array('logs','open', 'click', 'unsubscribe', 'soft', 'hard', );
foreach ($arrays as $key => $value) {
if (is_array($value)) {
foreach ($value as $item) {
$result[$item['isp']][$key] = $item['count'];
}
}
}
$results = array();
foreach($result as $key => $value)
{
foreach($keys as $k)
{
if(!isset($result[$key][$k])) $result[$key][$k] = 0;
}
}
return $result;
}
如果需要,这是我的动作控制器:
public function logsAction()
{
$log_review = new Dashboard_Model_DbTable_logs();
$open = $log_review->getOpen();
$click = $log_review->getClick();
$unsubscribe = $log_review->getUnsubscribe();
$logs = $log_review->getlogs();
$SB = $log_review->getSB();
$HB = $log_review->getHB();
$result = $this->array_merge_rec(array('logs'=>$logs,'open' => $open, 'click' => $click, 'unsubscribe' => $unsubscribe, 'hard' => $HB, 'soft' => $SB));
$this->view->data = $result;
}
正如我所说,我想做的是将所有查询结果合并到一个基于 ISP 的数组“$result”中。当我在没有 getlogs 查询的情况下合并所有查询时,该函数正常工作,但是当我将 getlogs 查询添加到函数参数时,它不起作用并且是正常的。所以我需要帮助来了解如何修改我的合并函数以获取一个数组,该数组包含所有查询的公共 ISP 列上合并的所有列