简体   繁体   中英

How to store MySQL data in .JSON file format?

The thing which I want to do is, to fetch a data (phone number) from MySQL, and store it in an array, which is defined in a file format .JSON!

I don't know any JSON so I am having this trouble. A function uses administrators.json file to get phone numbers, which is actually written in a array, like this:

[
  {
    "phone_number": "+1555555555",
    "name": "Foo"
  }
]

But I don't want static values, I want to retrieve phone number from MySQL, and then pass that number to this JSON array. How am I suppose to do that?

The function that uses this administrators.json is as follows:

 private function _notificationRecipients()
    {
        $adminsFile = base_path() .
            DIRECTORY_SEPARATOR .
            'config' . DIRECTORY_SEPARATOR .
            'administrators.json';
        try {
            $adminsFileContents = \File::get($adminsFile);

            return json_decode($adminsFileContents);
        } catch (FileNotFoundException $e) {
            Log::error(
                'Could not find ' .
                $adminsFile .
                ' to notify admins through SMS'
            );
            return [];
        }
    }

There is a function on php called json_encode which basically does the work for you. You can then write the result to a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM