Jump to content

Recommended Posts

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\"}"}]}





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);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.