I want to add this symbol [ ] to the column "Post_images"
with basis data sql server
Like this,
“post_images” : “ht tp://img*sample*com/gambar1*jpg”, “ht tp://img*sample*com/gambar2*jpg”
to
“post_images” : [“ht tp://img*sample*com/gambar1*jpg”, “ht tp://img*sample*com/gambar2*jpg”]
In json the [] indicates a set of items.
Assuming you have an array with both strings inside then calling json_encode(array) should add the [ ].
See also this tutorial for more detais.
http://www.tutorialspoint.com/json/json_php_example.htm
Please do not write json on your own. Use the php functions instead.
You have to place array for post_images
it mean if you have $data which you json_encodeing you should add your post images as sub array not string. Example
$data = array(
'param1' => "data ...",
'post_images' => array(
“ht tp://img*sample*com/gambar1*jpg”,
“ht tp://img*sample*com/gambar1*jpg”
);
);
echo json_encode($data); // will output what you where looking for
Do the the following:
'["ht tp://imgsamplecom/gambar1*jpg", "ht tp://imgsamplecom/gambar2*jpg"]'
Example:
http://sqlfiddle.com/#!9/971279/1
Related
The json column in the database has a data like this:
{"data": {"total":15,"time":25}}
This is the data I want to update within it:
{"total":22,"time":5}
But when updating the data via Laravel like this:
$json_data = json_encode(['total' => 22, ...]);
$table->where->update(['column->data' => $json_data])
the result is as follows:
{"data": "{\"total\":22,\"time\":5}"}
When I pass the array without json_encodeing it, it raises an error instead.
How can I assign a non-primitive value directly to a property in a JSON field?
I think the only solution is as follows:
$data = ['total' => 22, 'time' => 5];
$table->where->update([
'column->data->total' => $data['total'] ,
'column->data->time' => $data['time']
])
I searched to batch update the contents of a json key without double quotes but couldn't find it.
Either the content will be pulled completely and then edited and sent again. Or individual updates will be made as above. Please let me know if you find a better solution.
seems it got double json encoded, first you json_encode'ed it manually, then the $table->where->update-function json_encode'ed it again. try replacing
$json_data = json_encode(['total' => 22, ...]);
with just
$json_data = ['total' => 22, ...];
I have a proper .json physical file and i read it from PHP by parsing it.
Let's say the sales.json contains:
{
"custid" : "7761",
"items" : [
{
"itemcode" : "A11231G",
"suppliers" : [
{
"id" : "s10001",
"name" : "Benny & John",
},
{
"id" : "s10004",
"name" : "Colorado Dimension",
}
]
}
]
}
Then i consume it from PHP:
$sales = json_decode( file_get_contents("store/sales.json"), true );
There is no problem and $sales is already become an Array which is ok.
Now for some reason, i want to feed that json_decode() function with an PHP Array (instead of the .json physical file).
I know it is the dumb way that i am actually doing like converting, Array -> json -> Array, which is finally Array to Array.
But for whatever reason i have,
Even if i have a PHP Array() with the correct structure, if i use json_encode($phpArray), then to feed as json_decode( json_encode($phpArray), true ), will it give the exact object like i get from json_decode("sales.json") file?
(or) how can i feed the json_decode function with a PHP Array() object i have?
Actually, json_decode returns a STD object, not an array. To get an array from a JSON string you need to use json_decode($string, true).
I search a lot in stack and google try to find the answer which seems to be easy but I'm still stuck with it
I write a code to encode json with values I wanted from . and I would like to add a key / value to the JSON
the JSON is as following structure
{
- files: [
{
title: "works",
- tracks: [
{
title: "File",
format: "mp3"
}
]
},
-{
title: "season1",
tracks: [
{
title: "Button-1",
format: "wav"
},
-{
title: "Beep-9",
format: "wav"
}
]
}
]
}
I want to add to that a key and its value at the beginning to the json as properties under the title files , I mean that can be read by code as
json[files][new_key]
I tried to set that value like this
$json['new_key'] = "new_value";
but this causes adding numbers to the arrays in json , I don't why they numbered
this numbers affect my reading way of the json as JSONModel in my iOS app
so , I hope you can help me
thanks in advance
Assuming that the new value you want to add varies by file, you would need to loop through $json[files] and insert them per key/value pair.
<?php
for($i=0; $i<count($json); $i++)
$json[files][$i]["new_key"] = "value";
?>
I'm still not sure what you have exactly, but it seems you are trying to manipulate the json string.
If done correctly, that is probably the most efficient solution, but what you could also do is:
use json_decode to generate an array from your json string;
locate the correct section / sub-array where you want to add your data;
use array_unshift to prepend your new key - value pair;
use json_encode to generate a json string from your complete array.
The reason you're getting numbers appearing is because you're adding a key to an array (which functions more or less as a list in JS). So before you basically have the object "files" as a list of objects zero-indexed like any other JS array. When you add the key, JS simply adds your key to the end of your present keys (in your case 0 and 1).
It seems like you have a list of multimedia objects where each has a title and a list of tracks. The most straightforward way to solve your issue would be as follows:
$fileItem['title'] = 'works';
$fileItem['tracks'] = array(
array(
'title' => 'File',
'format' => 'mp3'
)
);
$json['files'][] = $fileItem;
$fileItem['title'] = 'season1';
$fileItem['tracks'] = array(
array(
'title' => 'Button-1',
'format' => 'wav'
),
array(
'title' => 'Beep-9',
'format' => 'wav'
)
);
$json['files'][] = $fileItem;
Then you JSON encode it and return it as you normally would. You can put the above in a loop as well. I lack enough context to recommend exactly how.
I use hightchartPHP to display chart. But being a bug, hope people take the time to read and help me.
$chart->series[] = array(
'name' => 'Sales',
'data' => array(1,2,3,4,5,6));
Code run.
[1,2,3,4,5,6]
But, i get data mysql
$chart->series[] = array(
'name' => 'Sales',
'data' => array($sales->monthly_sales()));
then automatic highchart add quote . Code not run.
["1,2,3,4,5,6"].
I tried by add json_encode. But code not run.
["\"1,2,3,4,5,6\""]
Try using JSON.parse before you send data (string) into a series data.
series : [{
data: JSON.parse(data)
}]
Because you get string with php not a javascript array of numbers! another easy and not good approach is to remove quotes from your php string $sales->monthly_sales() with something like this:
str_replace('"', "", $string);
I am building a simple messaging system, and i have a collection in mongodb with documents like this:
{ "_id" : ObjectId("50ad003f9811e5bc5c000000"), "between" : [ "user1,user2,user3" ] }
I want to perform this query:
db.conversations.find({'between': ["user1,user2,user3"]});
to get this exact document back. This query works in mongo shell.
in php-mongo, i tried this:
$collection->find(array("between"=>array("user1", "user2", "user3")));
but it does not work.
What am i doing wrong ?
Wouldn't you want to do an In query here?
db.collection.find( { "between" : { $in : ["user1", "user2", "user3"] } } );
See In query here:
Mongo Advanced $in query
making your PHP query look like:
$collection->find(array("between"=>array("$in"=>array("user1", "user2", "user3"))));
//untested, should be something similar to this.
or if you're trying to find it exactly wouldn't you just be able to do:
$collection->find(array("between"=>array("user1,user2,user3")));
First of all when you are saving your data you have to use array not a string
{ "between" : [ "user1,user2,user3" ] }
this stores in "between" an array of one element "user1,user2,user3"
Basically when you do your query in shell everything is ok, because you are asking for a array with one element. But in php you are asking for an array of three elements.
So, when you save your data, most probably that is what you need :
{ "between" : [ "user1","user2","user3" ] }
and this will give you an array of three users.
Then read the documentation http://www.mongodb.org/display/DOCS/Advanced+Queries to and adjust your query depends on what you need: either the exact array or only some elements in the array
Have you tried:
$collection->find(array("between"=>"user1,user2,user3"));
or
$collection->find(array( "$elemMatch" => array( "between"=>"user1,user2,user3" ));
The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches.
Consider the following example which uses the $or operator.
$collection->find([
'select' => ['$in' => ['option 1', 'option 2', 'option 3']]
]);
References