How to Add Square Braces Around Subarray Data Inside of a JSON Encoded String

How to add square braces around subarray data inside of a json encoded string?

You need to wrap things in another array:

$data = array("item" => array(array("id" => "123456", "name" => "adam")));

This will be more understandable if we use the equivalent PHP 5.4 array syntax:

$data = [ "item" => [ ["id" => "123456", "name" => "adam"] ] ];

Compare this with the JSON:

        { "item":   [ {"id":"123456", "name":"adam"      } ] }

The only thing to explain is why one of the PHP arrays remains an array [] in JSON while the other two get converted to an object {}. But the documentation already does so:

When encoding an array, if the keys are not a continuous numeric
sequence starting from 0, all keys are encoded as strings, and
specified explicitly for each key-value pair.

json_encode treats a single element array as object

At the moment, you have a single array with 2 elements, instead of an array with a single element of a sub-array. In order to get the json in the first section, you need to add another array level.

$data = [
"email" => $_POST['mail'],
"campaign" => [
"campaignId" => "4JIXJ"
],
"customFieldValues" => [
[
"customFieldId" => "y8jnp",
"value" => ["18-29"]
]
]
];

That will give you this:

{
"email": null,
"campaign": {
"campaignId": "4JIXJ"
},
"customFieldValues": [
{
"customFieldId": "y8jnp",
"value": ["18-29"]
}
]
}

PHP JSON Encode square bracket

In JSON [] is an array and {} is an object.

An array holds an ordered list of values.

An object holds an unordered group of key / value pairs.

If you want an array, then you have to provide an ordered list of values (a PHP array) and not a set of key / value pairs (a PHP associative array).


$data = [ "item" =>  ["id", "123456", "name", "adam"]  ];
$data_string = json_encode($data);

gives

{"item":["id","123456","name","adam"]}

place form data in multiples array and save in json format

You can easily build up an associative array in your desired format, then json_encode() it:

$formattedOutput = Array(
'student' => Array(
'arraypage1' => Array(
'name' => $input['name'],
'student' => $input['student'],
'email' => $input['email']
),
'arraypage2' => Array(
'format' => $input['format'],
'lists' => $input['lists']
),
'arraypage3' => Array(
'class_lists' => $input['class_lists'],
'status' => $input['status']
)
)
);

$form_attributes = json_encode($formattedOutput);

The exact key names may differ, but you should get the idea.

UPDATE:
To get the square brackets, you can wrap with additional Array():

$formattedOutput = Array(
'student' => Array(
Array('arraypage1' =>
Array(
Array(
'name' => $input['name'],
'student' => $input['student'],
'email' => $input['email']
)
)
),
Array('arraypage2' =>
Array(
Array(
'format' => $input['format'],
'lists' => $input['lists']
)
)
),
Array('arraypage3' =>
Array(
Array(
'class_lists' => $input['class_lists'],
'status' => $input['status']
)
)
)
)
);

see this post for more details: no square bracket json array

Square API Create Item working code now returning an error

Variations needs to be passed in as an array. Here is how the JSON should look:

{
"id":"test_1433281486",
"name":"test",
"description":"test",
"variations": [{
"id":"test_v1433281486",
"name":"Regular",
"price_money": {
"currency_code":"USD",
"amount":"19800"
}
}]
}

Thanks for the report! We will update our error messaging to send the proper message if variations is not passed in as an array.

Encode PHP array with non-numeric keys to JSON array

You can use array_values:

echo json_encode(array_values($a));

Message formatting in Slack in using blocks layout

I don't have enough reputation, but I believe this is a duplicate of: no square bracket json array

Also, this is not valid json, you can check on https://jsonlint.com/?code=

{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}

To summarize the post, all you really need to do is wrap your inner array with another array

$data = array(
'blocks' => array(array(
'type' => 'mrkdwn',
'text' => 'Danny Torrence left the following review for your property'
)),
);

this returns:

{"blocks":[{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}]}

Array instead of object from json_encode with recursive method

Given the (very) specific conditions stated in the question and the comments:

protected function stringKeyToMultArray(&$newarr, $keys, $value) {
if (count($keys) > 1) {
$key = array_shift($keys);
if ( !isset($newarr[$key]) || !is_array($newarr[$key])) {
$newarr[$key] = array(array());
}
$this->stringKeyToMultArray($newarr[$key][0], $keys, $value);

} else {
$newarr[array_shift($keys)] = $value;
}
}

see https://3v4l.org/4Hpek



Related Topics



Leave a reply



Submit