0

I'm looking into saving an array to a text file for later access, but i was wonder what way is better to save this array? Its really just an opinion question! I really just want to know what would be the better way to save this as well as why is there a null in there on the first part?

{
    "questionbanknames": "Question Bank Name",
    "question": [null, {
        "question": "This is question 1?",
        "type": "manual",
        "a": "",
        "b": "",
        "c": "",
        "d": ""
    }, {
        "question": "This is question 2?",
        "type": "multi",
        "a": "yes",
        "b": "no",
        "c": "maybe",
        "d": "all of the above"
    }]
}

OR

 "questionbanknames": "Question Bank Name",
        "question[1][question]": "This is question 1?",
        "question[1][type]": "manual",
        "question[1][a]": "",
        "question[1][b]": "",
        "question[1][c]": "",
        "question[1][d]": "",
        "question[2][question]": "This is question 2?",
        "question[2][type]": "multi",
        "question[2][a]": "yes",
        "question[2][b]": "no",
        "question[2][c]": "maybe",
        "question[2][d]": "all of the above"

2 Answers 2

1

I prefer storing JSON format without serializing. It makes it easier for multiple clients to consume it in the future. Also creating a PHP Object from a JSON string is a breeze.

So I would serialize if it is not JSON, I would save as-is if it is JSON.

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

2 Comments

That makes more sense, so just directly save the bottom array in this case?
@MarkShaw The bottom array should not be directly saved. Instead json_encode it and save the resultant JSON into a file. When you need the data back, simply read the file content into a variable and json_decode the variable. This will give you a PHP Object/Array which you can then work with.
0

As you have Json Encoded string I would suggest to store this in Database instead in a file. Encode before saving and get with json decode as array.

Handling JSON from Database much easier than storing in a file and fetching from file. what if you have thousands of questions.

Comments

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.