How to decode the following syntax in PHP array:
{
'Property': {
'Lang', 'en'
, 'Make': function () {
// Do something
}
}
}
where the value of "Make" is of type string
Finally getting a table like:
echo $ array ['property'] [ 'lang']; // String
echo $ array ['property'] [ 'Make']; // String
PLEASE : It's not a standard JSON that I use, See well the Make Property.
Do you mean the function json_decode()? You can read more about it here:
http://www.php.net/manual/en/function.json-decode.php
You can use json_decode(); in your code.
json_decode($json_result);
"$json_result" your data in json type.
For your knowledge
http://www.php.net/manual/en/function.json-decode.php
Related
I have below json data and decode it to display on the screen. When I check the type of the value, it shows array instead of object. How to get actual type of value in PHP.
JSON is
{ "allData" : { "image" : [], "contents": {.., "box": {}, "text":[]} } }
When I decode and parse the above JSON data the "allData", "contents", "box" type are shows as array instead of object. How can I get those type as object and "image" type as array. Please help.
Thanks,
Guru
This normally occurs when you are using the true option in the json_decode function.
For eg.,
$str = '{"allData":{"image":["img1.png"],"contents":{"title":"title name","box":{"name":["sample text 1","sample text2"]},"text":[]}}}';
var_dump(json_decode($str, true));
Just try to remove the true in the json_decode function and you should get an object.
Hope this helps.
If you use json_decode with the true option, it will return the result as an array.
Maybe you can check this link.
If you get "Cannot use object of type stdClass as array" the error, you can look at this answer.
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
Extract from the RFC 7159 (JSON) :
These are the six structural characters:
begin-array = ws %x5B ws ; [ left square bracket
begin-object = ws %x7B ws ; { left curly bracket
end-array = ws %x5D ws ; ] right square bracket
end-object = ws %x7D ws ; } right curly bracket
..
However: php allows you to treat the result as an array (of arrays)
so:
json_decode($json, true); // return as array
returns the result as an array.
and
json_decode($json)
gives you the result as Objects AND arrays . So given your example:
"allData" : { "image" : [], ..
returns a stdClass-Object with the field "image" of type array. The array is empty for your example.
So to retrieve all images, use something like:
$result=json_decode($json);
foreach($result->allData->image as $img) {
echo "found image: $img.";
}
I have a json string derived from MySQL query using GROUP_CONCAT (which returns STRING).
Here's JSON string as field portfolios:
[
{name:"Branch GBD_1.pdf", img_path:"1333752771.pdf"},
{name:"Doc.pdf", img_path:"2020107119.pdf"},
{name:"COK.pdf", img_path:"264860069.pdf"}
]
Now in php, I tried to decode the field and loop through it, but I am not being able to.
foreach($records as $r)
{
$varArray = json_decode($r['portfolios'], true);
foreach($varArray as $que)
{
echo $que['name'].' '.$que['img_path'];
echo '<br/>';
}
}
How do I break or convert the variable into loop-able object?
$varArray is not an array because your json string is not valid json. The keys need to be wrapped in doublequotes.
[
{"name":"Branch GBD_1.pdf", "img_path":"1333752771.pdf"},
{"name":"Doc.pdf", "img_path":"2020107119.pdf"},
{"name":"COK.pdf", "img_path":"264860069.pdf"}
]
I have this JSON encoded code in my mysql database:
{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}
And i want to query and decode it to echo it at my website.
You have to use a php "json_decode()" function to decode a json encoded data.
Basically json_decode() function converts JSON data to a PHP array.
Syntax: json_decode( data, dataTypeBoolean, depth, options )
data : - The json data that you want to decode in PHP.
dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". Both data types can be accessed like an array and use array based PHP loops for parsing.
depth :- Optional recursion limit. Use an integer as the value for this parameter.
options :- Optional JSON_BIGINT_AS_STRING parameter.
Now Comes to your Code
$json_string = '{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}';
Assign a valid json data to a variable $json_string within single quot's ('') as
json string already have double quots.
// here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.
/ just can check here your encoded array data.
// echo '<pre>';
// print_r($json_decoded_data);
// loop to extract data from an array
foreach ($json_decoded_data as $key => $value) {
echo "$key <br/>";
foreach($value as $k=>$data)
{
echo "$k | $data <br/>";
}
}
Evening everyone!
So, I have with me a JSON string...
{"username":"87db3983285d395ca0af9f","password":"f4f0bb1533ef5034ce6b0a8a7c49a43b","email":"xxx#gmail.com","hnum":3,"splicenum":22,"reg_ip":"71.126.122.217","reg_date":1364175245,"cur_ip":"71.126.122.217","ip_array":["71.126.122.217"],"logins":[],"about":""}
And I decode this string into an associative array in PHP using json_decode().
What I'm doing is trying to make 1 single function for querying a JSON object that has been converted to an associative array in PHP. For this function, I am now working on editing/updating fields.
Example:
json_edit(array(
"set"=>array(
"email"=>"yyy#yahoo.com"
)
));
The key "set" means setting the value of a string or boolean.
Another key would be "push" to append to an array, or "delete" to delete a string or an array.
What I'm wondering is, how can I get the data type of the current array part in PHP?
Meaning, how can I get PHP to say, "OK, the field 'username' is a string, and 'ip_array' is an array"?
I don't want to be able to "set" a string value to what is SUPPOSED to be just a boolean, or, an array.
Is there any way to get the JSON data types in PHP?
Thanks so much in advance.
What about gettype(). Gettype() will return the data type of any variable. http://php.net/manual/en/function.gettype.php
I would use something like this;
<?php
function json_edit($json, $changes = array()){
$decoded = json_decode($json);
foreach($changes as $action => $data){
swith($action){
case 'SET':
foreach($data as $key => $value){
$decoded[$key] = $value;
}
break;
case 'DELETE' :
break;
default;
}
}
return json_encode($decoded);
}
?>
This question already has answers here:
json_decode to array
(12 answers)
Closed 11 months ago.
The json data object below is what's returned from a custom Google search API request. I need to extract each of the "url" elements and place them into an array (using PHP).
myArray = {url1, url2, url3, etc...}
How?
data = '{
"responseData":
{
"results":
[
{
//etc
}
]
}
Am I right that you have JSON string? Use json_decode to decode it. After that you can use
array_map(function($x){
return $x->url;
},$var->responceData->results);
(Requires PHP 5.3 for anonymous function, you can use no anonymous ones if use PHP5.2 or older)
For later versions:
function smth($x){
return $x->url;
}
array_map('smth',$var->responceData->results);
You can use json_decode to get an array corresponding to your JSON and then analyze it like you would do for a normal array.
You might want to read up on json_decode
Try using:
$myObject=json_decode($myJSONstring);
Here's the reference.
Then do:
$urlArray=array();
foreach($myObject->responseData->results as $myResult) {
foreach($myResult as $myAttribute => $myValue) {
$urlArray[] = $myValue;
}
}
$urlArray will be what you're looking for.