I have a JS literal object string such as {name:{first:"George",middle:"William"},surname:"Washington"}
and I have to convert it in Json. How can I do it using PHP?
JS:
// Pretend we're POSTing this
var foo = {foo:{first:"George",middle:"William"}};
PHP:
$foo = $_POST['foo'];
$foo = json_decode( stripslashes( $foo ) );
echo $foo->first;
Credit where credit is due: https://www.youtube.com/watch?v=pORFYsgOXog
If someone is still looking for an easy solution to this, as I did recently, you could check out the PHP library that I wrote: ovidigital/js-object-to-json
1) Install with composer
composer require ovidigital/js-object-to-json
2) Use it inside your project
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($javascriptObjectString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With