I have a string (JSON type), i wanted to convert it to PHP Array.
{
"action":"putEntity",
"dataPacket":{
"entity":[
{
"name":"product",
"data":[
{ }
]
}
]
}
}
I did following to do so,
$array = json_decode(json_encode($data), True);
When i do var_dump($array); it displays:
string(1578) "{ "action": "putEntity", "dataPacket": { "entity": [{ "name": "product", "data": [{ }] }] } }"
But when i do, print_r($array); it displays:
{
"action": "putEntity",
"dataPacket":{
"entity":[
{
"name": "product",
"data":[{}]
}
]
}
}
Issue is when i try to print $array['dataPacket']; it throws error illegal string offset 'dataPacket'
why var_dump is showing it as String? please help.
$array = json_decode(json_encode($data), True);
Should be
$array = json_decode($data, true);
Related
I have a json response like below:
$response ='[
{
"userSummaries": [
{
"id": "9910",
"status": "Active",
"name": "Jhon"
}
]
},
{
"userSummaries": [
{
"id": "8754",
"status": "Active",
"name": "Jane"
}
]
}
]';
and I would like to group this by userSummaries with this php code:
$myArr = json_decode($response, true);
$result_arr = [];
array_walk($myArr,function($v,$k) use (&$result_arr){
$result_arr[key($v)] = $v[key($v)];
});
echo json_encode($result_arr);
and the response only return one data:
{"userSummaries":[{"id":"8754","status":"Active","name":"Jane"}]}
Is it possible to get the output response like this?:
{"userSummaries":[{"id":"9910","status":"Active","name":"Jhon"}, {"id":"8754","status":"Active","name":"Jane"}, ]}
Tried over the net but I did not found the solutions
here my script for this: https://3v4l.org/tVkK5
also tried this:
$class_array = array();
foreach ($myArr as $sa) {
$class_array[$sa['userSummaries']][] = array('name' => $sa['name']);
}
but return:
Notice: Undefined index: name in /in/hvSFC on line 28
Warning: Illegal offset type in /in/hvSFC on line 28
Notice: Undefined index: name in /in/hvSFC on line 28
Warning: Illegal offset type in /in/hvSFC on line 28
[]
need help
You were close. You just needed to reference the key and first of userSummaries in each loop, instead of working with the whole...
$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $user) {
$result_arr["userSummaries"][] = $user['userSummaries'][0];
}
echo json_encode($result_arr);
Results in:
{"userSummaries":[
{"id":"9910","status":"Active","name":"Jhon"},
{"id":"8754","status":"Active","name":"Jane"}
]}
If you foresee that userSummaries in each will have multiple users themselves... then this would work:
$response ='[
{
"userSummaries": [
{
"id": "9910",
"status": "Active",
"name": "Jhon"
}
]
},
{
"userSummaries": [
{
"id": "8754",
"status": "Active",
"name": "Jane"
},
{
"id": "5421",
"status": "Active",
"name": "Bob"
}
]
}
]';
$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $usergroup) {
foreach($usergroup['userSummaries'] as $user) {
$result_arr["userSummaries"][] = $user;
}
}
echo json_encode($result_arr);
Results in:
{"userSummaries":[
{"id":"9910","status":"Active","name":"Jhon"},
{"id":"8754","status":"Active","name":"Jane"},
{"id":"5421","status":"Active","name":"Bob"}
]}
Hello I am trying to break down JSON data
** {
"message": "gradeExam.php",
"id": "171",
"student_id": "dfd",
"questions": [{
"question_id": "0",
"student_input": "def doubly_"
},
{
"question_id": "1",
"student_input": "asd"
}
]
}**
using PHP
$json = file_get_contents('php://input');
$_POST = json_decode($json, true);
$input = $_POST["questions"];
foreach($input as $questions)
{
$quest_id[] = $questions["question_id"];
$student_input[] = $questions["student_input"];
}
echo $quest_id;
echo $student_input;
BUT keep getting a response of.. ArrayArrayArray[]
I am trying loop and get the data of question_id: 0, student_input: def doubly_, question_id: 1, student_input: asd.
What am I doing wrong?
Everything that you have done is correct except these 2 lines.
echo $quest_id;
echo $student_input;
As $quest_id and $student_input are arrays we cannot use the echo function. Instead, you can use the print_r function as below
print_r($quest_id);
print_r($student_input);
This is the JSON
{
"circuit_list": [
{
"_id": "58c0f378a986f808cdaf94cf",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
},
{
"_id": "58c0f378a986f808cdaf94d0",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
}
}
I already try with this code
$json = json_decode($url, true);
foreach($json as $value)
{
$_id = $value->_id;
}
it didn't work. Please help, I need to get the value to show them on the view. Did I do this wrong? this json is difficult because i didn't understand the structure.
I usually decode json with format
[{"id":"1","name":"faisal"}]
like this and with my foreach it's working.
If the second parameter of json_decode is true, the function will return an array instead of an object. Also, you would need to loop over the circuit_list property of the object.
$json = json_decode($url); // <- remove the parameter
foreach($json->circuit_list as $value) // <- loop over circuit_list
{
$_id = $value->_id;
}
<?php
$json = json_decode($url,true);
foreach($json['circuit_list'] as $value)
{
$id = $value['_id'];
}
?>
I;m trying to get data from a website using the following code:
<?php
$url = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=4798';
$content = file_get_contents($url);
var_dump($content);
$json = json_decode($content, true);
var_dump($json);
for ($idx = 0; $idx < count($json); $idx++) {
$obj = (Array)$json[$idx];
echo 'result' . $obj["name"];
}
?>
Which is getting me this result:
string(0) "" NULL
<?php
$url = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=4798';
$content = file_get_contents($url);
echo "<pre>";
//print_r($content);
$data = json_decode($content);
print_r($data); //Show the json decoded data comes form $url
##Parse this array {$data} using foreach loop as your use
?>
There are no numeric keys in the json returned from the url you posted in your question. So iterating through the associative array with numeric keys returns nothing.
This is the structure of the json you are working with:
{
"item": {
"icon": "http://services.runescape.com/m=itemdb_oldschool/5122_obj_sprite.gif?id=4798",
"icon_large": "http://services.runescape.com/m=itemdb_oldschool/5122_obj_big.gif?id=4798",
"id": 4798,
"type": "Default",
"typeIcon": "http://www.runescape.com/img/categories/Default",
"name": "Adamant brutal",
"description": "Blunt adamantite arrow... ouch.",
"current": {
"trend": "neutral",
"price": 529
},
"today": {
"trend": "neutral",
"price": 0
},
"members": "true",
"day30": {
"trend": "negative",
"change": "-9.0%"
},
"day90": {
"trend": "negative",
"change": "-20.0%"
},
"day180": {
"trend": "negative",
"change": "-31.0%"
}
}
}
Try accessing $json["item"]. That should give you something more meaningful to work with. If you want to iterate over the key/value pairs in the item, use a foreach loop:
foreach($json["item"] as $key => $value) {
echo $key . ":";
print_r($value);
}
I am test-running a web application
where in json is required. I have the following json
structure:
{
"Articles": [
{
"Article": {
"ID": 111,
"title": "Idiot",
"author": "Moron",
"pubDate": "11/2/14",
"summary": "bla bla bla"
},
"Article": {
"ID": 222,
"title": "wisdom",
"author": "wise one",
"pubDate": "11/2/15",
"summary": "ha ha ha"
}
}
]
}
I then decided to check if a matching ID exits before adding
any record. To this effect, I wrote a method, encased it within
a JSon class as follows:
public function ID_Exists($ID){
$file = file_get_contents($this->FileName, true);
$data = json_decode($file, false); //get json in array string format
foreach($data as $child){
foreach($child as $item){
if($item->ID == $ID){
echo 'Exists';
}else{
echo 'Non Existent';
}
}
}
}
I test-ran it like:
$Obj = new JSon('file.json'); //knows what to do
if($Obj->ID_Exists(111)){
//ok ! no problem
}else{
////no problem
}
Here's the output I got:
Undefined property: stdClass::$ID in
C:\Server\wamp\www\Oweb\libs\dmanager.php on line 635
What am I doing wrong? I don't want to use the array
format of json_decode().
Your JSON structure is impossible. Articles is an array which contains a single object which contains the key Article twice - this cannot work.
Your structure needs to be:
{
"Articles": [
{
"ID": 111,
...
},
{
"ID": 222,
...
}
]
}
Which you can traverse using:
$data = json_decode($json);
foreach ($data->Articles as $article) {
if ($article->ID == ..) ..
}