【问题标题】:How do i remove this from an array()?我如何从数组()中删除它?
【发布时间】:2016-03-20 17:18:54
【问题描述】:

如何从 $this->v 中删除“dato”?并且仍然在 $sql 字符串中。

输出 (var_dump($this->v)):

array (size=4)
  ':kategori' => string 'Youstube' (length=8)
  ':link' => string 'http://urls' (length=11)
  ':navn' => string 'Rss feeds' (length=9)
  ':dato' => string 'NOW()' (length=5)

输出(回显 $sql)

UPDATE rss_kategori SET kategori = :kategori, link = :link, navn = :navn, dato = NOW()

代码:

$classVars = get_class_vars(get_class($this));
            $sql .= "UPDATE {$this->table} SET ";
            foreach ($classVars as $key => $value) {
                if ($key == 'v' || $key == 'db' || $key == 'id' || $key == 'table' || $key ==
                    'table_id') {
                    continue;
                }
                $keys = "";
                $values = "";
                $keys .= $key;
                if ($this->$key == 'NOW()') {
                    $values .= $this->$key . ",";
                } else {
                    $values .= ":" . $key . ",";
                }
                $this->v[":" . $key] = $this->$key;
                $sql .= "{$keys} = " . $values . " ";

            }

            $sql = substr($sql, 0, -2) . " WHERE {$this->table_id} = '{$this->id}'";

【问题讨论】:

标签: php mysql arrays pdo


【解决方案1】:

请试试这个:

$key = ':dato';
$arr = $this->v;
if (array_key_exists($key, $this->v) {
     unset($arr[$key]);
     $this->v = $arr;
}

【讨论】:

  • 我可以检查值NOW()是否存在于数组中?
  • 是的@nodde。 if (in_array('NOW()', $this->v)) { echo "exists"; } else { // Blah blah }
【解决方案2】:

尝试使用unset() 函数。

unset($this->v['dato']);

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2021-01-30
    • 2011-10-25
    相关资源
    最近更新 更多