NotionCommotion Posted November 13, 2016 Share Posted November 13, 2016 How can create $string='{"foo":123,"a1":[{"x":321,"y":{"a":5,"b":"hello"}},{"x":321,"y":{"a":2,"c":"goodby"}},{"x":321,"y":{"a":5,"b":"hi","d":"please"}}]}' given $a2? I would rather not convert all the individual JSON strings to an array or object first, and then convert it all back to JSON. Maybe another option (yikes!) is manually creating the JSON string? Note that foo will always be an integer. <?php $a1=[ ['x'=>321,'y'=>'{"a":5,"b":"hello"}'], ['x'=>321,'y'=>'{"a":2,"c":"goodby"}'], ['x'=>321,'y'=>'{"a":5,"b":"hi","d":"please"}'] ]; $a2=['foo'=>123,'a1'=>$a1]; print_r($a2); echo(json_encode($a2)."\n"); echo(json_encode($a2,JSON_UNESCAPED_SLASHES)."\n"); echo(json_encode($a2,JSON_UNESCAPED_UNICODE)."\n"); echo(json_encode($a2,JSON_UNESCAPED_SLASHES,1)."\n"); echo(json_encode($a2,JSON_UNESCAPED_UNICODE,1)."\n"); echo(json_encode($a2,JSON_UNESCAPED_SLASHES,2)."\n"); echo(json_encode($a2,JSON_UNESCAPED_UNICODE,2)."\n"); echo(json_encode($a2,JSON_UNESCAPED_SLASHES,3)."\n"); echo(json_encode($a2,JSON_UNESCAPED_UNICODE,3)."\n"); OUTPUT Array ( [foo] => 123 [a1] => Array ( [0] => Array ( [x] => 321 [y] => {"a":5,"b":"hello"} ) [1] => Array ( [x] => 321 [y] => {"a":2,"c":"goodby"} ) [2] => Array ( [x] => 321 [y] => {"a":5,"b":"hi","d":"please"} ) ) ) {"foo":123,"a1":[{"x":321,"y":"{\"a\":5,\"b\":\"hello\"}"},{"x":321,"y":"{\"a\":2,\"c\":\"goodby\"}"},{"x":321,"y":"{\"a\":5,\"b\":\"hi\",\"d\":\"please\"}"}]} {"foo":123,"a1":[{"x":321,"y":"{\"a\":5,\"b\":\"hello\"}"},{"x":321,"y":"{\"a\":2,\"c\":\"goodby\"}"},{"x":321,"y":"{\"a\":5,\"b\":\"hi\",\"d\":\"please\"}"}]} {"foo":123,"a1":[{"x":321,"y":"{\"a\":5,\"b\":\"hello\"}"},{"x":321,"y":"{\"a\":2,\"c\":\"goodby\"}"},{"x":321,"y":"{\"a\":5,\"b\":\"hi\",\"d\":\"please\"}"}]} {"foo":123,"a1":[{"x":321,"y":"{\"a\":5,\"b\":\"hello\"}"},{"x":321,"y":"{\"a\":2,\"c\":\"goodby\"}"},{"x":321,"y":"{\"a\":5,\"b\":\"hi\",\"d\":\"please\"}"}]} {"foo":123,"a1":[{"x":321,"y":"{\"a\":5,\"b\":\"hello\"}"},{"x":321,"y":"{\"a\":2,\"c\":\"goodby\"}"},{"x":321,"y":"{\"a\":5,\"b\":\"hi\",\"d\":\"please\"}"}]} Quote Link to comment https://forums.phpfreaks.com/topic/302531-including-json-in-json_encode-without-adding-escape-characters/ Share on other sites More sharing options...
requinix Posted November 14, 2016 Share Posted November 14, 2016 It seems a bit silly but I'd still do the two conversions: I object to piecing the JSON together by hand on principle, and decoding the strings doesn't take that much code. $a1 = [ ['x' => 321, 'y' => '{"a":5,"b":"hello"}'], ['x' => 321, 'y' => '{"a":2,"c":"goodby"}'], ['x' => 321, 'y' => '{"a":5,"b":"hi","d":"please"}'] ]; $a2 = ['foo' => 123, 'a1' => $a1]; array_walk($a2['a1'], function(&$v) { $v['y'] = json_decode($v['y']); }); echo json_encode($a2); Quote Link to comment https://forums.phpfreaks.com/topic/302531-including-json-in-json_encode-without-adding-escape-characters/#findComment-1539293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.