This question already has an answer here:
php - converting from one json format to another
(1 answer)
Closed 1 year ago.
I've been trying to add a parent key "data" to a PHP $dataset json:
my $dataset json:
[
{
"id":"H",
"description": "Hello"
},
{
"id":"B",
"description":"Bye",
},
]
the final output must be
[
"data": {
{
"id":"H",
"description": "Hello"
},
{
"id":"B",
"description":"Bye",
},
},
]
Can you please help me? Thank you!
The easiest way is to decode to array and add the parent then encode again to json.
$output_json = json_encode(array("data" => json_decode($dataset)));
$data = [
'data' => $dataset,
'status' => true
];
return json_encode($data);
The syntax of JSON type is {key:vale}
your code should be like:
{
"data": [
{
"id":"H",
"description": "Hello"
},
{
"id":"B",
"description":"Bye",
},
],
}
Try this out:
$final_result = array('data' => $dataset)
Related
What would be the equivalent PHP array structure to create an object with identical properties:
For example... create the object 'columns' below in PHP using json_encode:
jQuery('#example').dataTable( {
"ajaxSource": "sources/objects.txt",
"columns": [
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
]
} );
(I am trying to build a dynamic datatable and define the columns in the source JSON.
You could use an ArrayObject
new ArrayObject([
"ajaxSource" => "...",
"columns" => [
new ArrayObject(['data' => 'engine']),
new ArrayObject(['data' => 'browser']),
new ArrayObject(['data' => 'etc'])
]
]);
if you want to assemble this you need to store the objects inside an array like
$columns = [];
for(...) {
$columns[] = new ArrayObject(['data' => 'etc']);
}
Have a look at http://php.net/manual/de/arrayobject.construct.php
This question already has answers here:
Append data to JSON array using PHP
(3 answers)
Closed 4 months ago.
I have a JSON file with an array called "foods". Inside this array I want to push a new object with name, type and price.
{
"foods":[
{
"name": "Name1",
"tyoe": "Type1",
"price": "Price1"
},
{
"name": "Name2",
"type": "Type2",
"price": "Price2"
}
]
}
The logic is:
I receive from $_POST a name, type and price. (I'ts a simple form -- it's working ok)
So I want to push these data with PHP inside the "foods" array in JSON file.
The result would be like this:
{
"foods":[
{
"name": "Name1",
"tyoe": "Type1",
"price": "Price1"
},
{
"name": "Name2",
"type": "Type2",
"price": "Price2"
},
{
"name": "NAME3",
"type": "TYPE3",
"price": "PRICE3"
}
]
}
Wrong example that I'm trying to fix it:
$name = $_POST['name'];
$type = $_POST['type'];
$price = $_POST['price'];
//Do I need to create this array to push inside JSON array "foods"?
$arrayFoods = array(
'name' => $name,
'type' => $type,
'price' => $price
);
$my_file = file_get_contents('database/file.json');
//Do I need to decode the JSON file to access the array "foods" to push my $arrayFoods?
//How do I access the "foods" array in JSON file and push my $arrayFoods?
$fileDecode = json_decode($my_file);
Thanks!
Following what you have done
$food = new stdClass;
$food->name = $name;
$food->type = $type;
$food->price = $type;
$my_file = file_get_contents('database/file.json');
$fileDecode = json_decode($my_file);
array_push($fileDecode->foods, $food);
//to save
file_put_contents('database/file.json', json_encode($fileDecode));
I am trying to create mongoDB subdocuments record inside PHP code,
{
"_id": "",
"ref": [
{
"crm_base_contact_id": "1653",
"crm_imported_files_id": "906"
}
],
"data": [
{
"First_name": "Annalee",
"Last_name": "Graleski",
},
{
"First_name": "Henry",
"Last_name": "Smith",
}
],
}
How to create two arrays inside "data" subdocuments in php code .
Please provide me any idea to insert this in mongoDB using PHP code,
You can add additional fields with the $set operator when updating a document:
db.collection.update({},{"$set": { "history": "value" }})
If you want that field to be an array or sub-document then it is just the same:
db.collection.update({},{"$set": { "history": ["a","b","c"] }})
If you are struggling with converting the JSON syntax into php code, then here is something that will help you in the future:
$result = '{"$set": { "history": ["a","b","c"] }}';
echo var_dump( json_decode( $result ) );
$test = array( '$set' => array('history' => array( 'a', 'b', 'c') ) );
echo json_encode( $test ) ."\n"
So the first line just decodes the json and will dump what it should look like. If you are not sure, then json_encode your php array declaration to see that you have it right.
I know this question was asked already for couple times, I've seen the answers but it already helped me a lot, but i need to solve one more problem regarding to this.
So the question is:
I need to build json file with php.
Here how looks my json file that i need:
{
"fashion":[
{
"alt":"Alisa",
"src":"img/fashion/Alisa/kubik.jpg",
"class":"albumItem",
"id":"FashionAlbum001",
"itemNum":0,
"album":[
{
"alt":"albumImg1",
"src":"img/fashion/Alisa/alisa1.jpg"
},
{
"alt":"albumImg1",
"src":"img/fashion/Alisa/alisa5.jpg"
},
{
"alt":"albumImg1",
"src":"img/fashion/Alisa/alisa7.jpg"
}
]
},
{
"alt":"2-Addis",
"src":"img/fashion/2-Addis/kubik.jpg",
"class":"albumItem",
"id":"FashionAlbum002",
"itemNum":1,
"album":[
{
"alt":"albumImg1",
"src":"img/fashion/2-Addis/addis1.jpg"
},
{
"alt":"albumImg4",
"src":"img/fashion/2-Addis/addis4.jpg"
}] } ] }
and so on...
I can't find out how to make a list of images inside each album
This is a php function a have
function buildJson(){
$json = json_encode(array(
"Fashion" => array(
"alt" => "Alisa",
"src" => "img/fashion/Alisa/kubik.jpg",
"id" => "FashionAlbum001",
"itemNum"=>"1",
"album"=>array(
"src"=>"img/fashion/Alisa/alisa1.jpg",
),
array(
"src"=>"img/fashion/Alisa/alisa5.jpg",
),
array(
"src"=>"img/fashion/Alisa/alisa7.jpg",
),
)
));
echo $json;
}
but I get json like this one:
{
"Fashion": {
"0": {
"src": "img/fashion/Alisa/alisa2.jpg"
},
"1": {
"src": "img/fashion/Alisa/alisa3.jpg"
},
"alt": "Alisa",
"src": "img/fashion/Alisa/kubik.jpg",
"id": "FashionAlbum001",
"itemNum": "0",
"album": {
"src": "img/fashion/Alisa/alisa1.jpg"
}
}
}
How is it possible to fix it?
Thank you!
Please pay more attention to the code you're writing :) Try to decode correct version of your json file and compare it to one you wrote. You should see some differences.
Your problem ir what follows after album key. You are assigning array with only one value to it instead of assigning array of arrays.
This is the way to go:
"album" => array(
array("src" => "img/fashion/Alisa/alisa1.jpg"),
array("src" => "img/fashion/Alisa/alisa5.jpg"),
array("src" => "img/fashion/Alisa/alisa7.jpg"),
),
you trouble in nesting album array
fixed code
function buildJson(){
$json = json_encode(
array(
"Fashion" => array(
"alt" => "Alisa",
"src" => "img/fashion/Alisa/kubik.jpg",
"id" => "FashionAlbum001",
"itemNum"=>"1",
// nesting error here
"album"=> array(
array("src"=>"img/fashion/Alisa/alisa1.jpg"),
array("src"=>"img/fashion/Alisa/alisa5.jpg"),
array("src"=>"img/fashion/Alisa/alisa7.jpg")
)
)
)
);
echo $json;
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 11 months ago.
All,
I have the following JSON Data. I need help writing a function in PHP which takes a categoryid and returns all URLs belonging to it in an array.
Something like this::
<?php
function returnCategoryURLs(catId)
{
//Parse the JSON data here..
return URLArray;
}
?>
{
"jsondata": [
{
"categoryid": [
20
],
"url": "www.google.com"
},
{
"categoryid": [
20
],
"url": "www.yahoo.com"
},
{
"categoryid": [
30
],
"url": "www.cnn.com"
},
{
"categoryid": [
30
],
"url": "www.time.com"
},
{
"categoryid": [
5,
6,
30
],
"url": "www.microsoft.com"
},
{
"categoryid": [
30
],
"url": "www.freshmeat.com"
}
]
}
Thanks
What about something like this :
You first use json_decode, which is php's built-in function to decode JSON data :
$json = '{
...
}';
$data = json_decode($json);
Here, you can seen what PHP kind of data (i.e. objects, arrays, ...) the decoding of the JSON string gave you, using, for example :
var_dump($data);
And, then, you loop over the data items, searching in each element's categoryid if the $catId you are searching for is in the list -- in_array helps doing that :
$catId = 30;
$urls = array();
foreach ($data->jsondata as $d) {
if (in_array($catId, $d->categoryid)) {
$urls[] = $d->url;
}
}
And, each time you find a match, add the url to an array...
Which means that, at the end of the loop, you have the list of URLs :
var_dump($urls);
Gives you, in this example :
array
0 => string 'www.cnn.com' (length=11)
1 => string 'www.time.com' (length=12)
2 => string 'www.microsoft.com' (length=17)
3 => string 'www.freshmeat.com' (length=17)
Up to you to build from this -- there shouldn't be much left to do ;-)
Try the built-in json_decode function.