如果我输入以下内容,get_tag_id将带回第一个数组项:
texture,clients
我得到了这个回报:
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 :(得分: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,它将不再打印第一个数组两次。