A. Volg
A. Volg

Reputation: 307

PHP/Laravel: create JSON with a particular Key name

I am using imagick to get a number of pages and save it to json. But I store the number as is: for example - 20. I need to store the number with a key like this:

    {
       "pages": "20"
    }

Here is the code example, that I have. $json - just a path to a created json file. $num - number of pages.

 Storage::put(($json), $num);

Should I creat json first, and then somehow add key to a number and encode file?

Upvotes: 1

Views: 1821

Answers (1)

crishoj
crishoj

Reputation: 5927

    /**
     * Write the contents of a file.
     *
     * @param string $path
     * @param string|resource $contents
     * @param mixed $options
     * @return bool 
     * @static 
     */ 
    public static function put($path, $contents, $options = array())
    {
        return \Illuminate\Filesystem\FilesystemAdapter::put($path, $contents, $options);
    }

The second parameter to Storage::put is the raw contents you wish to store in the given path. So yes, you have to encode your metadata in your desired format first.

E.g.:

$data = ['pages' => $num];
Storage::put($jsonPath, json_encode($data));

Upvotes: 1

Related Questions