How to add another object with value in data?
Something start like :
$data=[{name:"apple"}]
And i wanted output like this
$data=[{name:"apple",city:"gotham"}]
Dont try and build JSON manually, create a PHP data structure that you want and then use json_encode() to make it into a JSON String
$d = [(object)['name' => 'apple', 'city' => 'gotham']];
echo json_encode($d);
RESULT
[{"name":"apple","city":"gotham"}]
If some values already exists, you should decode it to a PHP data struture and then add to it and convert back to JSON String
$data='[{"name":"apple"}]';
$d = json_decode($data);
$d[0]->city = 'Gotham';
$data = json_encode($d);
RESULT
[{"name":"apple","city":"Gotham"}]
Related
I have a json file:
$json='[{"Email":"myemail1#domain.com","Name":"company 1","Tel1":"xx-xx-xx","Adresse":"XXXXXX"},{"Email":"myemail2#domain.com","Name":"Company 2","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}]';
and my forms post data in variable
vars="fname"=>"Ameur","lname"=>"KHIL"
"fname"=>"Marak","lname"=>"Cristo"
and I like to insert in between my json content with variable to get the final json like this:
$result='[{"Email":"myemail1#domain.com","Name":"company 1","vars":{"fname":"Ameur","lname":"KHIL","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}},{"Email":"myemail2#domain.com","Name":"Company 2","vars":{"fname":"Marak","lname":"Cristo","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}}]';
For this purpose you can use json_decode() to parse the JSON-String to a PHP-Object. Then you just set a new value vars to the given form values.
Parsing the JSON-String
$json = json_decode('[{"Email":"myemail1#domain.com","Name":"company 1","Tel1":"xx-xx-xx","Adresse":"XXXXXX"},{"Email":"myemail2#domain.com","Name":"Company 2","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}]');
Adding the new vars value and removing the additional ones. This is just for the first entry but you can do the same for the other entry or even iterate through the array for multiple entries
$json[0]->vars = ["fname" => "Marak", "lname" => "Cristo", "Tel1" => $json[0]->Tel1,"Adresse" => $json[0]->Adresse];
unset($json[0]->Tel1);
unset($json[0]->Adresse);
And getting your result in a JSON-String
$result = json_encode($json);
[{"id":2,"name":"Paypal"},{"id":3,"name":"Scrill"}]
How to add dynamically new attribute and its value in here using php or in laravel framework. converted data is like
[{"id":2,"name":"Paypal","amount":"100"},{"id":3,"name":"Scrill","amount":"200"}]
In pseudo code:
Convert JSON string to PHP associative array.
Add / alter keys on PHP associative array.
Convert PHP associative array to JSON string.
I don't want to take all the fun away - so here are some links:
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-encode.php
;-)
Update: OP has requested a complete solution.
To convert the JSON string to a PHP associative array:
$decoded = json_decode($jsonStr, true);
Always check that things didn't go wrong:
if ($decoded === null) {
die('Unable to decode JSON string: ' . $jsonStr);
}
Modify your PHP array:
$decoded[] = array(
'id' => 3,
'name' => 'Scrill',
'amount' => 200
);
Finally re-encode to a JSON string:
echo json_encode($decoded);
I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.
How can I read a json array and add/merge a new element to it?
The content of my file data.json looks like this array:
[["2015-11-24 18:54:28",177],["2015-11-24 19:54:28",178]]
new element array example:
Array ( [0] => Array ( [0] => 2015-11-24 20:54:28 [1] => 177 ) )
I used explode() and file() but it failed (delimiter for index 1..)..
has someone another idea or it is the right way to solve?
Firstly you need to import JSON content to your application as a string what can be done with file_get_contents().
After that you have to decode--or "translate"--JSON format to PHP primitives via json_decode(). The result will be the expected array to be handled.
Then you may append a new item to that array using [] suffix eg. $a[] = $b;
These three steps are exemplified below.
// get raw json content
$json = file_get_contents('data.json');
// translate raw json to php array
$array = json_decode($json);
// insert a new item to the array
$array[] = array('2015-11-24 20:54:28', 177);
In order to update the original file you have to encode PHP primitives to JSON via json_encode() and can write the result to desired file via file_put_contents().
// translate php array to raw json
$json = json_encode($array);
// update file
file_put_contents('data.json', $json);
<?php
$c = file_get_contents('data.json');
// <- add error handling here
$data = json_decode($c, true);
// <- add error handling here, see http://docs.php.net/function.libxml-get-errors
see http://docs.php.net/file_get_contents and http://docs.php.net/json_decode
I want to decode the array value in web service.I need to decode 'CONTENT_VALUES' field which has name and value as its object and pass it in json.ID and USER_ID return correct value(separate field) but name and question returns null. I want to decode 'ques' => $datas->name, 'answer' => $datas->value.value of CONTENT_VALUES=[{"name":"radio","value":"","sur_id":"3","user_id":"admin#gmail.com","pagename":"question_response"},{"name":"true","value":""}]
$query1 = mysql_query("select * from `$prefix.response` where ID='$sur_id'");
while ($fetch = mysql_fetch_array($query1)) {
$content = $fetch['CONTENT_VALUES'];
$datas = json_decode($content);
$test[] = array('ID' => $fetch['ID'], 'USERID' => $fetch['USER_ID'], 'ques' => $datas->name, 'answer' => $datas->value);
}
Take a look at http://php.net/manual/en/function.json-decode.php.
This will describe how to convert a JSON string into a PHP object or array.
When you get stuck more with this you can fairly simple google "php decode json" and find plenty helpful resources.
Edit: Read the question too quick just before and understood it wrong, have you tried checking the return result of your $datas = json_decode($content) statement? Does CONTENT_VALUES actually contain JSON encoded data? Does the decoded object actually contain name and value?
May be you should set use mb_convert_encoding before decoding like this
$content = $fetch['CONTENT_VALUES'];
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content_value);