2

Alright, my situation is a slightly odd one.

Consider a string in php which has its contents as follows:

[ ["BWS - Bondi Junction", "Westfield Shopping Centre, 530 Oxford Street", "Bondi Junction NSW 2022", "(02) 8035 9239", "1", -33.8912952, 151.2515329, "","1557", ["27-04-2015 to 03-05-2015","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","10:00 AM - 10:00 PM"], ["04-05-2015 to 10-05-2015","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","9:00 AM - 10:00 PM","10:00 AM - 10:00 PM"]]];

Note that the semicolon above is also a part of the string along with the brackets. All of it is inside a php variable of type string. Now, I need to create an actual php array from this string which looks like a javascript array (if it were echoed to the browser).

I cannot use javascript in any way so I cannot echo this to the browser and then pass it on to php using request. I have to do it within php itself.

Anyone can think of a clever trick to achieve this?

1 Answer 1

5

Of course, without the ; semi-colon, its still a valid JSON string, so just load it into json_decode, it'll work just fine:

$string = rtrim($string, ';'); // trim the semi-colon
$data = json_decode($string, true);

Sample Output

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

1 Comment

Thank you! Works! So stupid that I didn't realize this lol. Thanks again!

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.