This question already has answers here:
Read JSON Data Using PHP [duplicate]
(5 answers)
Closed 7 years ago.
I am unable to get values of array of this json... how can i have all these values seperately!!
{
"id": "jai",
"pwd": "123",
"user": [
{
"fname": "jai",
"lname": "gupta"
},
{
"fname": "sameer",
"lname": "seth"
}
],
"college": "vit"
}
$myArray = json_decode($json, true);
var_dump($myArray['id']);
var_dump($myArray['user'][0]['fname']);
You can decode these values from json to object.
$result = json_decode('{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}');
and acccess like below:
$result->id;
$result->pwd;
You can access each "value" by accessing regular php array key value pairs.
$jsonn = '{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}';
$new = json_decode($jsonn, true);
$id = $new['id'];
$user = $new['user'];
..... and so on.
Hope this helps.
Cheers!
Related
This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 14 days ago.
I'm trying to filter array from DB and I've got this postman response:
{
"1": {
"id": "3",
"key": "emails_html_body_start",
"value": "value"
}}
How I can access to id, key, value?
My code here:
$start = array_filter($array, function ($var) {
return ($var['key'] == 'emails_html_body_start');
});
echo json_encode($start);
Your question is a bit unclear ... So the upper code is what is sent by the lower code snippet? So the cho json_encode($start); is what produces the upper json data?
If so, then you obviously need to json decode that data again to be able to access a property inside that structure:
<?php
$input = <<<JSON
{
"1": {
"id": "3",
"key": "emails_html_body_start",
"value": "value"
}
}
JSON;
$data = json_decode($input, true);
$output = $data[1]['id'];
print_r($output);
The output obviously is:
3
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I have a json that I need to filter a specific key and value from
the following json
{
"5": {
"Owner": "94EAC",
"Record":"0121ln"
},
"15": {
"Owner": "009AC",
"Record":"0120Pc"
},
"1": {
"Owner": "00G11A",
"Record":"000lPcn"
},
"199": {
"Owner": "00G1y9",
"Record":"01211cn"
},
"33": {
"Owner": "001AC",
"Record":"0121n"
}
}
I would like to be able to pass the first int and get back array for that number.
For example if I pass 15 I get
{
"Owner": "009AC",
"Record":"0120Pc"
}
I tried foreach loop but cannot set specific value for the first int
If I assign $data = json
then $date[15] didn't work
$data->15 also didn't work
I did also use the json decode and was able to print an array but wasn't able to get a single value
Any help would be great, I did spend all day and still cannot get an answer.
Thank you
Using Array:
$arr = json_decode($json, true);
print_r( $arr['15']);
Using Object:
$obj = json_decode($json);
print_r( $obj['15']);
Reference: json_decode
This question already has answers here:
How can I access a deep object property named as a variable (dot notation) in php?
(5 answers)
Closed 4 years ago.
I have a .json configuration file. Using PHP, I'm trying to get its contents as an object by json_decode() and validate it. The JSON file contains multi-dimensional data. The problem is with accessing a member in depth dynamically.
For instance, consider the following data:
{
"somebody": {
"name": "Ali",
"age": 13,
"life": {
"stat": "good",
"happy": true
}
}
How to access the value of the following dynamically?
$happy = $data->somebody->life->happy;
What I mean from a dynamic access is something like this:
$happyIndex = "somebody->life->happy";
$happy = $data->{$happyIndex};
Also, I don't want to use eval().
Thanks.
Assuming you have a JSON data return
data="{
"somebody": {
"name": "Ali",
"age": 13,
"life": {
"stat": "good",
"happy": true
}
}"
data = jQuery.parseJSON(data);
then you can navigate to different parts of data by navigating
var name= data.somebody[0].name;
var age= data.somebody[0].age;
You will need a $.each function if you have more than one data in "somebody" array.
PHP Version:
$data='{"somebody":{"name": "Ali","age": "13","life": {"stat": "good","happy": "true"} }}';
$data = json_decode($data,TRUE);
$name= $data['somebody']['name'];
$age= $data['somebody']['age'];
echo("<pre>");
echo($name);
echo($age);
echo("</pre>");
//OUTPUT RESULT Ali 13
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
How do I get the uname value from this JSON into PHP variable? to use through my page?
{
"token": "iutiutiut-0jjjjj0-97987g",
"auth": {
"id": 1,
"app_id": 1,
"user": {
"uname": "foo",
"role": "member"
}
}
}
thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.
could you also tell me the correct terminology so I know as well thanks
What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:
$obj = json_decode($json_string);
$uname = $obj->auth->user->uname;
Or, if you prefer the array syntax, use the second argument in json_decode:
$arr = json_decode($json_string, true);
$uname = $obj['auth']['user']['uname'];
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parse a JSON string using PHP
this is my data object
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt"
},
{
"translatedText": "Hallo Berlin"
}
]
}
}
how do I parse this using PHP?
this is a jsonObject that contains jsonObject("data") that contains jsonArray that contains jsonObjects at each index that contains key/value "translatedText"
this is what I have and my assumption
$jsonResult = json_decode($data);
$translated_text = $jsonResult->data->translations[0]->translatedText;`
$array = json_decode($json_element, true);
to make associative array.
I'm think this is what it would be. json_decode does not parse to a PHP object, but to just an array.
$jsonResult = json_decode($data);
$translated_text = $jsonResult['data']['translations'][0]['translatedText'];
$array = json_decode($json_element);