Sunday, October 28, 2018

php - return single array from json by passing a value





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


Answer



Using Array:
$arr = json_decode($json, true);
print_r( $arr['15']);


Using Object:
$obj = json_decode($json);
print_r( $obj['15']);


Reference: json_decode


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...