JSON不是数组(JSON not an array)

我已经创建了一个类来处理我网站中的JSON操作。 我正在尝试以数组的形式将事件的标记添加到我的JSON文件中,但是我得到一个错误,说array_values和array_push期望1个参数是一个数组。 我不确定为什么会发生这种情况,因为变量已分配给数组。 有什么建议么? 目前,JSON文件为空。

class tagHandler { private $tagsFile = "tags.json"; private $LstTags; function __construct() { $this->LstTags = array (); if (! file_exists ($this->tagsFile)){ $fHND = fopen($this->tagsFile, "w"); $tmpArray = array(array("EID","EName","EColor", "EDel")); fwrite($fHND, json_encode($tmpArray)); fclose($fHND); } $encodedInput = file ($this->tagsFile); $this->LstTags = json_decode ($encodedInput[0]); } function __destruct(){ $this->update(); } public function addTag($EName,$EColor){ $this->LstTags = array_values($this->LstTags); array_push($this->LstTags, array($this->LstTags[count($this->LstTags)-1][0] + 1, $EName, $EColor, 0)); $this->update(); } public function update(){ $this->LstTags = array_values($this->LstTags); $fHND = fopen($this->tagsFile, "w"); fwrite($fHND, json_encode($this->LstTags)); fclose($fHND); //empty memory region $this->LstTags = array(); $encodedInput = file ( $this->tagsFile ); $this->LstTags = json_decode ( $encodedInput[0] ); } }

呼叫:

require "jsonclass.php"; $TagManager = new tagHandler(); $TagManager->addTag($StrName, $StrColor);

错误:

[07-Feb-2017 22:14:33 Europe/London] PHP Warning: array_values() expects parameter 1 to be array, null given in /home/thomassm/public_html/functions/php/jsonclass.php on line 24 [07-Feb-2017 22:14:33 Europe/London] PHP Warning: array_push() expects parameter 1 to be array, null given in /home/thomassm/public_html/functions/php/jsonclass.php on line 25 [07-Feb-2017 22:14:33 Europe/London] PHP Warning: array_values() expects parameter 1 to be array, null given in /home/thomassm/public_html/functions/php/jsonclass.php on line 30

谢谢

I've created a class to handle JSON actions within my website. I'm trying to add a tag for an event in the form of an array to my JSON file, however I get an error saying that array_values and array_push expect 1 parameter to be an array. I'm not sure why this is happening as the variable is assigned to an array. Any suggestions? Currently, the JSON Files is empty.

class tagHandler { private $tagsFile = "tags.json"; private $LstTags; function __construct() { $this->LstTags = array (); if (! file_exists ($this->tagsFile)){ $fHND = fopen($this->tagsFile, "w"); $tmpArray = array(array("EID","EName","EColor", "EDel")); fwrite($fHND, json_encode($tmpArray)); fclose($fHND); } $encodedInput = file ($this->tagsFile); $this->LstTags = json_decode ($encodedInput[0]); } function __destruct(){ $this->update(); } public function addTag($EName,$EColor){ $this->LstTags = array_values($this->LstTags); array_push($this->LstTags, array($this->LstTags[count($this->LstTags)-1][0] + 1, $EName, $EColor, 0)); $this->update(); } public function update(){ $this->LstTags = array_values($this->LstTags); $fHND = fopen($this->tagsFile, "w"); fwrite($fHND, json_encode($this->LstTags)); fclose($fHND); //empty memory region $this->LstTags = array(); $encodedInput = file ( $this->tagsFile ); $this->LstTags = json_decode ( $encodedInput[0] ); } }

Call:

require "jsonclass.php"; $TagManager = new tagHandler(); $TagManager->addTag($StrName, $StrColor);

Error:

[07-Feb-2017 22:14:33 Europe/London] PHP Warning: array_values() expects parameter 1 to be array, null given in /home/thomassm/public_html/functions/php/jsonclass.php on line 24 [07-Feb-2017 22:14:33 Europe/London] PHP Warning: array_push() expects parameter 1 to be array, null given in /home/thomassm/public_html/functions/php/jsonclass.php on line 25 [07-Feb-2017 22:14:33 Europe/London] PHP Warning: array_values() expects parameter 1 to be array, null given in /home/thomassm/public_html/functions/php/jsonclass.php on line 30

Thanks

最满意答案

删除文件,然后重试。 它必须工作。

并且,当然

$this->LstTags = json_decode ($encodedInput[0], true); if (!$this->LstTags) $this->LstTags = array();

并且更好地使用它

file_put_contents($this->tagsFile, json_encode($this->LstTags));

remove file and try again. its must work.

And, of couse

$this->LstTags = json_decode ($encodedInput[0], true); if (!$this->LstTags) $this->LstTags = array();

And better use this

file_put_contents($this->tagsFile, json_encode($this->LstTags));

更多推荐