0

When users keep interacting with my web app, the pages keep doing calls so that some info can be stored in a cookie linked file that is stored on the server (with the PHP session file, it gets complex: I don't have choice to see a complete history, etc.)

So, anyway, with every ajax call, I create json_encoded data in a flat file with the unique cookie name.

Data example of one such cookie file: (keys/values may repeat)

{"name":"John Doe"}
{"email":"[email protected]"}
{"location":"Disneyland, Orlando"}
{"country of residence":"Iceland"}
{"name":"Gill Bates"}
{"email":"[email protected]"}

Now, when I am searching this file, to retrieve data and to do other stuff, I would like to have the entire contents in one big array as if the entire data ws json_encoded in one shot.

How do I do this? I could do a bunch of str_replace and replace the "{" and "}" and get rid of the carriage returns but that seems to be a bit ugly

Is there a better way?

Thanks

1 Answer 1

1

I suggest to fix your json format by using str_replace as you saying because this is the only way to have a valid json format on using this function:

$str = '{"name":"John Doe"}
{"email":"[email protected]"}
{"location":"Disneyland, Orlando"}
{"country of residence":"Iceland"}
{"name":"Gill Bates"}
{"email":"[email protected]"}';
$fix_json = "[" . str_replace("}\n{", "},{", $str) . "]";

and after this you can decode you json and convert it with json_decode, like this:

$dataArray = json_decode($fix_json, true);
Sign up to request clarification or add additional context in comments.

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.