Thomas Smyth
Thomas Smyth

Reputation: 522

JSON not an array

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

Upvotes: 0

Views: 247

Answers (2)

Xakki
Xakki

Reputation: 180

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

Upvotes: 1

DCrystal
DCrystal

Reputation: 721

    $this->LstTags = json_decode ($encodedInput[0]);

You need to pass true as a second parameter to json_decode in order to receive an array back.

Edit: in case your file is empty and, consequently, $encodedInput[0] is an empty string, json_decode would return null. You can return an empty array in such cases:

    $this->LstTags = json_decode ($encodedInput[0], true) ?: [];

Upvotes: 0

Related Questions