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')
);
Related
I am sending request using curl.
this is json data :
{
"users": [
{
"userId": "123",
"userLink" : "www.example .com",
"userType": "au"
}
]
}
I am trying this but i need same above json:
$post = array(
"users" => array(
"userId" => "123",
"userLink" => "link",
"userType" => "au"
)
);
$out = array_values($post);
$post = json_encode($out);
print_r($post);
You need an additional array dimension, and if you use brackets [ ] you can easily see how they look the same:
$post = [
"users" => [
[
"userId" => "123",
"userLink" => "link",
"userType" => "au"
]
]
];
$post = json_encode($post);
Also, don't use array_values; that will remove the string key users.
Fist of all i would like to tell I am a beginner for JSON. I would like to know how to make a following format JSON using PHP. I think I want to use loop function.
Example:
[ { "id" : "1", "name" : "test1" },
{ "id" : "2", "name" : "test2" },
{ "id" : "3", "name" : "test3" },
{ "id" : "4", "name" : "test4" },
{ "id" : "5", "name" : "test5" } ]
In my PHP file have $VALUE variable in different places. I want know id and name values store method to make this JSON. I can not use $VALUE1, $VALUE2 etc.
Some places have following $VALUE variable with some data. is it method right to make this JSON
$VALUE = array("id" => "1", "name" => "test1");
$VALUE = array("id" => "3", "name" => "test3");
I think that json_encode() and json_decode() will help you.
$array = [
["id" => 1, "name" => "test1"],
["id" => 2, "name" => "test2"],
["id" => 3, "name" => "test3"],
["id" => 4, "name" => "test4"]
];
$jsonEncoded = json_encode($array);
$jsonDecoded = json_decode($jsonEncoded);
var_dump("Json Encoded array to string:", $jsonEncoded);
var_dump("Json Decoded string to array:", $jsonDecoded);
In order to update (add/update or remove) the encoded json array you need to decode first and encode again. There is no other way to add an "object" to an already encoded json string.
You can always check the official documentation for more information.
json_encode
json_decode
You can do it like this if you wanted : using arrays and with cast to an object
$json_php_oupout = [
(object)["id"=>"1", "name"=>"test1"],
(object)["id"=>"2", "name"=>"test2"],
(object)["id"=>"3", "name"=>"test3"],
(object)["id"=>"4", "name"=>"test4"],
(object)["id"=>"5", "name"=>"test5"]
];
var_dump($json_php_oupout);
if you want VALUE as an array to it like this adding manually indexes or with a loops.
$VALUE[0] = (object)array("id" => "1", "name" => "test1");
$VALUE[1] = (object)array("id" => "3", "name" => "test3");
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
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 am fetching data from DB like this
$endResult = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
if (!isset($endResult[$row['car']])) {
$endResult[$row['car']]= (object) array(
'car' => $row['car'],
'carModel' => $row['carModel'],
'colors' => array()
);
}
$endResult[$row['car']] -> colors [] = (object) array(
'paintedOn' => $row['paintenOnDate'],
'paintedBy' => $row['paintedBy']
);
}
//return with slim.php
$response->body(json_encode($endResult));
and result I am getting
{"1":
{
"car": "1",
"carModel": "model-1",
"colors": [
{
"paintedOn": "2014-11-07",
"paintedBy": "5"
},{
"paintedOn": "2014-11-08",
"paintedBy": "6"
}]
},
"2":{
"car": "2",
"carModel": "model-2",
"colors": [
{
"paintedOn": "2014-11-09",
"paintedBy": "7"
},{
"paintedOn": "2014-11-10",
"paintedBy": "8"
}]
}
}//<--replace this with []
Even if $endResult is declared as Array I am getting {} brackets, how could I replace "Object" brackets with "Array" brackets?
UPDATE: I can't remove json_encode as the front-end (backbone) expecting collection
UPDATE 2: $endResult = array(); return [...]
but this $endResult[$row['car']]= (object) array(...) convert it to {...}
You can't achieve what you want because it would result in invalid JSON. According to json.org:
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
So you can only have values in an array. Because you are adding a name to the value, it must become an object.
If you really want your JSON to be wrapped in an array you need to remove the first-level names, in your example "1" and "2":
[
{
"car": "1",
"carModel": "model-1",
"colors": [
{
"paintedOn": "2014-11-07",
"paintedBy": "5"
},
{
"paintedOn": "2014-11-08",
"paintedBy": "6"
}
]
},
{
"car": "2",
"carModel": "model-2",
"colors": [
{
"paintedOn": "2014-11-09",
"paintedBy": "7"
},
{
"paintedOn": "2014-11-10",
"paintedBy": "8"
}
]
}
]
Remove the conversion to JSON. Also "declaration" in PHP doesn't matter. You can still assign different types in the course of your program.
I think the PHP function json_decode Should help you here. This turns the JSON format into an array.
use json_decode($data,true) for convert returned data in to array.
simply use array_values function:
$array = array_values($response);