2011-06-13 99 views
1

的get_tag_id被帶回第一陣列項目兩次,如果我輸入以下內容:PHP:的foreach循環次數太多

質地,客戶

我得到這樣的回報:

Array 
(
    [0] => texture 
    [1] => clients 
) 
Array 
(
    [0] => stdClass Object 
     (
      [TagID] => 4 
      [Title] => texture 
     ) 

) 
Array 
(
    [0] => stdClass Object 
     (
      [TagID] => 4 
      [Title] => texture 
     ) 

) 
Array 
(
    [0] => stdClass Object 
     (
      [TagID] => 1 
      [Title] => clients 
     ) 

) 
Array 
(
    [0] => texture 
    [1] => clients 
) 

以下是代碼:

 // Break up Tags and Store in an Array 
     $delimiter = " "; 
     $tags = explode($delimiter, $this->input->post('tags')); 

     // Remove Accidential Spaces if any. 
     $tags = array_filter($tags, function($var){ 
      return preg_match('/^[a-z-]+$/i', $var); 
     }); 

     // Create Arrays for Seperation 
     $newTags = array(); 
     $currentTags = array(); 
     print_r($tags); 

     // Check if Tags Exists 
     foreach ($tags as $tag) 
     { 
      if (!$this->Bookmark_model->tag_exists($tag)) 
      { 
       array_push($currentTags, strtolower($tag)); 
       // Get ID of tags that are existing 
       foreach ($currentTags as $tagname) 
       { 
        $id = $this->Bookmark_model->get_tag_id($tagname); 
        echo $id; 
       } 
      } 
      else 
      { 
       array_push($newTags, strtolower($tag)); 
      } 
     } 

get_tag_id()的代碼:

function get_tag_id($tag) 
{ 
    // Load Database 
    $this->load->database(); 
    // Query Database 
    $this->db->where_in('Title', $tag); 
     $query = $this->db->get('Tag', 1); 
     $result = $query->result(); 
     // Return ID of Tag 
     //return $result[0]->TagID; 
     print_r($result); 
} 

回答

0

其因爲這段代碼就在這裏:

  array_push($currentTags, strtolower($tag)); 
      // Get ID of tags that are existing 
      foreach ($currentTags as $tagname) 
      { 
       $id = $this->Bookmark_model->get_tag_id($tagname); 
       echo $id; 
      } 

取出的foreach,它將不再打印第一陣列的兩倍。