1

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 [];
        }
    }

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

but the file is having .json format! Can I run php code in it? Will I have to change the .json to .php?
I thought you just wanted to create a JSON from the data retrieved from the database? Or am I getting things wrong.
@ThesK you don't run code in the json file, you read/write the json file with PHP. json_encode converts a PHP array to a JSON string. json_decode converts a JSON string to a PHP array.
@DebetWah Yes, but for retrieving the data(phone number) I will need to run the query right!? Where I am suppose to write this query? in the notificationRecipients function?
you could do it in any php script. You just wanted to generate the JSON file right?
|

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.