2013-11-08 3 views
0

Я работаю над проектом»в Zend Framework, и я пытаюсь объединить t'he результат моих запросов SQL в одном массиве, как $result здесь мои запросы:Объединить массивы рекурсивно PHP

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

и t'his t'he функции, что я сделал, чтобы сделать t'he слияния:

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. И когда я объединяю все запросы без запроса getlogs, функция работает нормально, но когда я добавляю запрос getlogs к параметру функции, это не работает, и это нормально. Так что мне нужна помощь, чтобы знать, как я могу изменить функцию слияния, чтобы получить массив, который содержит все колонки объединены в общую колонку ISP для всех запросов

ответ

1

Проверки Kohana_Arr класс, в частности, как методы:

.. ..............

/** 
    * Merges one or more arrays recursively and preserves all keys. 
    * Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)! 
    * 
    *  $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane')); 
    *  $mary = array('name' => 'mary', 'children' => array('jane')); 
    * 
    *  // John and Mary are married, merge them together 
    *  $john = Arr::merge($john, $mary); 
    * 
    *  // The output of $john will now be: 
    *  array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane')) 
    * 
    * @param array initial array 
    * @param array array to merge 
    * @param array ... 
    * @return array 
    */ 
    public static function merge(array $a1, array $a2) 
    { 
     $result = array(); 
     for ($i = 0, $total = func_num_args(); $i < $total; $i++) 
     { 
      // Get the next array 
      $arr = func_get_arg($i); 

      // Is the array associative? 
      $assoc = Arr::is_assoc($arr); 

      foreach ($arr as $key => $val) 
      { 
       if (isset($result[$key])) 
       { 
        if (is_array($val) AND is_array($result[$key])) 
        { 
         if (Arr::is_assoc($val)) 
         { 
          // Associative arrays are merged recursively 
          $result[$key] = Arr::merge($result[$key], $val); 
         } 
         else 
         { 
          // Find the values that are not already present 
          $diff = array_diff($val, $result[$key]); 

          // Indexed arrays are merged to prevent duplicates 
          $result[$key] = array_merge($result[$key], $diff); 
         } 
        } 
        else 
        { 
         if ($assoc) 
         { 
          // Associative values are replaced 
          $result[$key] = $val; 
         } 
         elseif (! in_array($val, $result, TRUE)) 
         { 
          // Indexed values are added only if they do not yet exist 
          $result[] = $val; 
         } 
        } 
       } 
       else 
       { 
        // New values are added 
        $result[$key] = $val; 
       } 
      } 
     } 

     return $result; 
    } 

    /** 
    * Overwrites an array with values from input arrays. 
    * Keys that do not exist in the first array will not be added! 
    * 
    *  $a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon'); 
    *  $a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer'); 
    * 
    *  // Overwrite the values of $a1 with $a2 
    *  $array = Arr::overwrite($a1, $a2); 
    * 
    *  // The output of $array will now be: 
    *  array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos') 
    * 
    * @param array master array 
    * @param array input arrays that will overwrite existing values 
    * @return array 
    */ 
    public static function overwrite($array1, $array2) 
    { 
     foreach (array_intersect_key($array2, $array1) as $key => $value) 
     { 
      $array1[$key] = $value; 
     } 

     if (func_num_args() > 2) 
     { 
      foreach (array_slice(func_get_args(), 2) as $array2) 
      { 
       foreach (array_intersect_key($array2, $array1) as $key => $value) 
       { 
        $array1[$key] = $value; 
       } 
      } 
     } 

     return $array1; 
    } 

.........

/** 
* Tests if an array is associative or not. 
* 
*  // Returns TRUE 
*  Arr::is_assoc(array('username' => 'john.doe')); 
* 
*  // Returns FALSE 
*  Arr::is_assoc('foo', 'bar'); 
* 
* @param array array to check 
* @return boolean 
*/ 
public static function is_assoc(array $array) 
{ 
    // Keys of the array 
    $keys = array_keys($array); 

    // If the array keys of the keys match the keys, then the array must 
    // not be associative (e.g. the keys array looked like {0:0, 1:1...}). 
    return array_keys($keys) !== $keys; 
} 

/** 
* Test if a value is an array with an additional check for array-like objects. 
* 
*  // Returns TRUE 
*  Arr::is_array(array()); 
*  Arr::is_array(new ArrayObject); 
* 
*  // Returns FALSE 
*  Arr::is_array(FALSE); 
*  Arr::is_array('not an array!'); 
*  Arr::is_array(Database::instance()); 
* 
* @param mixed value to check 
* @return boolean 
*/ 
public static function is_array($value) 
{ 
    if (is_array($value)) 
    { 
     // Definitely an array 
     return TRUE; 
    } 
    else 
    { 
     // Possibly a Traversable object, functionally the same as an array 
     return (is_object($value) AND $value instanceof Traversable); 
    } 
} 
+0

Спасибо! ваша функция is_assoc - это классическая функция для проверки того, является ли массив ассоциативным? – Ahmadhc

+1

Добавлены другие необходимые методы, в любом случае проверьте класс kohana arr. – rernesto

Смежные вопросы