1

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?

1 Answer 1

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

2 Comments

and then if I want to add in another function the second key with an array? smth like: {"files":"20", "map": { "key": "value", etc.}} using PHP. Where can I find info about working with json's ?
Simply add more entries to the $data array before passing it to json_encode(). E.g.: $data = ['files' => 20, 'map' => ['key => 'value', 'etc' => '...']]

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.