简体   繁体   English

如何以.JSON文件格式存储MySQL数据?

[英]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! 我想要做的是,从MySQL获取数据(电话号码),并将其存储在一个数组中,该数组以文件格式.JSON定义!

I don't know any JSON so I am having this trouble. 我不知道任何JSON所以我遇到了这个麻烦。 A function uses administrators.json file to get phone numbers, which is actually written in a array, like this: 一个函数使用administrators.json文件来获取电话号码,这实际上是用数组写的,如下所示:

[
  {
    "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. 但我不想要静态值,我想从MySQL检索电话号码,然后将该号码传递给这个JSON数组。 How am I suppose to do that? 我怎么想这样做?

The function that uses this administrators.json is as follows: 使用此administrators.json的函数如下:

 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. php上有一个名为json_encode的函数,它基本上可以为你工作。 You can then write the result to a file. 然后,您可以将结果写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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