How to Create Json Like This In Php?
[
[
"16226281",
"11",
"Disclosure.1994.720p.BluRay.H264.AAC-RARBG",
"finished"
],
[
"16226038",
"140",
"Courage The Cowardly Dog (1999-2002)",
"finished"
],
[
"16226020",
"101",
"[R.G. Mechanics] Call of Duty 4 Modern Warfare",
"finished"
]
]
I search a lot on the internet but couldn't get any result.
$arr = [
[
"16226281",
"11",
"Disclosure.1994.720p.BluRay.H264.AAC-RARBG",
"finished"
],
[
"16226038",
"140",
"Courage The Cowardly Dog (1999-2002)",
"finished"
],
[
"16226020",
"101",
"[R.G. Mechanics] Call of Duty 4 Modern Warfare",
"finished"
]
]
echo json_encode($arr);
Follow below link :
http://php.net/manual/en/function.json-encode.php
Just create an array
<?php
$arr = array(
array(
"16226281",
"11",
"Disclosure.1994.720p.BluRay.H264.AAC-RARBG",
"finished"
),
# ....
);
and for output as json
<?php echo json_encode($arr);?>
Here is an example of JSON encoding using the inbuilt json_encode PHP function which can create JSON formatted data from PHP array:
<?php
$arr = array(
'key 1' => "value 1",
'key 2' => "value 2",
'key 3' => "value 3"
);
echo json_encode($arr);
?>
Source: How to create JSON formatted text in PHP
Related
I am trying to pass some JSON keys/values that I have to another JSON I am creating dynamically.
For example, this is the JSON I have in $json_create
{
"Key 1":"Value 1",
"Key 2":"Value 2",
"Key 3":"Value 3",
"Key 4":"Value 4"
}
That comes over file_get_contents
$requestBody = file_get_contents('php://input');
$json_create= json_decode($requestBody);
And this is the JSON I am creating
$final_json = [
$json_create,
"type" => "message",
"elements" => $json_merge,
"actions" => $actions_json
];
echo json_encode($final_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
Which print something like
{
"type": "message",
"elements": [
{
"msg_text": "This is a simple response message"
}
]
}
What I am trying to achieve is
{
"Key 1":"Value 1",
"Key 2":"Value 2",
"Key 3":"Value 3",
"Key 4":"Value 4",
"type": "message",
"elements": [
{
"msg_text": "This is a simple response message"
}
]
}
There is quite a lot on that subject but somehow I could not succeed in implementing it.
Use array_merge rather than putting the $json_create inside the array.
$final_json = array_merge($json_create,
[
"type" => "message",
"elements" => $json_merge,
"actions" => $actions_json
]);
<?php
$json_create = '{"Key1": "Value 1", "Key2": "Value 2", "Key3": "Value 3", "Key4": "Value 4"}';
$json_create = (array) json_decode($json_create);
$your_array = [
"type" => "message",
"elements" => 'foo',
"actions" => 'bar'
];
$final_json = array_merge($json_create, $your_array);
$result = json_encode($final_json);
echo $result;
output
{
Key1: "Value 1",
Key2: "Value 2",
Key3: "Value 3",
Key4: "Value 4",
type: "message",
elements: "foo",
actions: "bar"
}
This can be done using union operator also.
$requestBody = '{
"Key 1":"Value 1",
"Key 2":"Value 2",
"Key 3":"Value 3",
"Key 4":"Value 4"
}';
$json_create= json_decode($requestBody, true );
$other_array = [
"type" => "message",
"elements" => [],
"actions" => []
];
$final_json = $json_create + $other_array;
i have the created the following object array in php
$treeData[] = (object) array(
"name"=> "A",
"children" => [
[
"name"=> "A1",
"children"=> [
[
"name"=> "A1.1",
"children"=> [
[
"name"=> "A1.1.1",
"children"=> [
....
I'm trying to push new values inside the children of A1.1.1 based on below condition:
foreach ($treeData as $value) {
if ($value->name == 'A') {
$value->name[][] = (object) array(
"name"=> "ChildA",
"children"=> ""
);
break;
}
}
But it's giving me an error
Expected result should match as below example:
$treeData[] = (object) array(
"name"=> "A",
"children" => [
[
"name"=> "A1",
"children"=> [ [
"name"=> "A1.1",
"children"=> [ [
"name"=> "A1.1.1",
"children"=> [
[
"name"=> "ChildA",
"children"=> [ [
"name"=> "ChildA1"
] ]
],
[
"name"=> "ChildA",
"children"=> [ [
"name"=> "ChildA2"
] ]
],
]
] ]
] ]
]
]);
What I'm doing wrong here or any way to achieve this in different approach
The error I'm getting:
"Fatal error: Uncaught Error: [] operator not supported for strings "
First of all you getting the error as you do: $value->name[][]. Notice the name is a string so you ccannot use [] (array append operator) on it.
I would have take the recursive approach if I were you. Consider the following pseudo code:
function addChild($root, $addToName, $nameToAdd) {
if ($root->name == $addToName)
$root->children[] = (object) array("name"=> $nameToAdd, "children"=> []);
else
foreach($root->children as $child)
addChild($child, $addToName, $nameToAdd);
}
And now call is with: addChild($treeData, 'A', "ChildA")
How to Create Json Like This In Php?
[
[
"16226281",
"11",
"Disclosure.1994.720p.BluRay.H264.AAC-RARBG",
"finished"
],
[
"16226038",
"140",
"Courage The Cowardly Dog (1999-2002)",
"finished"
],
[
"16226020",
"101",
"[R.G. Mechanics] Call of Duty 4 Modern Warfare",
"finished"
]
]
I search a lot on the internet but couldn't get any result.
$arr = [
[
"16226281",
"11",
"Disclosure.1994.720p.BluRay.H264.AAC-RARBG",
"finished"
],
[
"16226038",
"140",
"Courage The Cowardly Dog (1999-2002)",
"finished"
],
[
"16226020",
"101",
"[R.G. Mechanics] Call of Duty 4 Modern Warfare",
"finished"
]
]
echo json_encode($arr);
Follow below link :
http://php.net/manual/en/function.json-encode.php
Just create an array
<?php
$arr = array(
array(
"16226281",
"11",
"Disclosure.1994.720p.BluRay.H264.AAC-RARBG",
"finished"
),
# ....
);
and for output as json
<?php echo json_encode($arr);?>
Here is an example of JSON encoding using the inbuilt json_encode PHP function which can create JSON formatted data from PHP array:
<?php
$arr = array(
'key 1' => "value 1",
'key 2' => "value 2",
'key 3' => "value 3"
);
echo json_encode($arr);
?>
Source: How to create JSON formatted text in PHP
I'm trying to replicate this json by converting a php array with json_encode:
"sort" : [
{ "age" : "desc" },
"_score"
]
With the following PHP array:
$json_doc['sort'] = array(
"_score",
"age"=>"desc"
);
If I just have the score in the array, PHP properly does not assign a key to the array:
"sort" : [
"_score"
]
But when I mix the "age"=>"desc" into the mix, json_encode does not encode the _score correctly:
"sort": {
"0": "_score",
"age": "desc"
}
$json_doc['sort'] = Array(
"_score",
Array('age' => 'desc')
);
in my d3js example contain [ symbol after children key & working perfectly
{"name": "root",
"children": [
{"name": "typeA(1095)",
"children": [
{"name": "2010(365)",
"children": [
{"name": "january(31)", "value": 31},
http://bl.ocks.org/mbostock/1283663
https://gist.github.com/mbostock/1283663/raw/a05a94858375bd0ae023f6950a2b13fac5127637/readme.json
but i convert a multi-dimensional array using php json_encode there is no [ symbol so when i pass it to d3js example it's not working
$json = array('name'=>'p_date','children'=>array('name'=>'HedCET','value'=>10));
json_encode($json) output
{"name":"p_date","children":{"name":"HedCET","value":10}}
there is no [ symbol after children key, is there any additional option to enable [ symbol in php json_encode ?
OR is there any difference between d3js json file and php json file ?
In PHP
'children' => array('name' => 'HedCET', 'value' => 10)
is
"children": {"name": "HedCET", "value": 10}
in JSON.
You have forgotten to put another array():
'children' => array( array('name' => 'HedCET', 'value' => 10) )