0

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

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Where do you suggest I put the file_put_contents? In the update function? Further more, as I call the class from various locations the JSON file appears in various directories. How can I fix this?
0
    $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) ?: [];

2 Comments

edited the answer, missed that the file exists, but is empty
Care to explain what's the downvote for - did I miss anything?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.