Why I am getting Notice: Undefined index: CIRReportData JSON PHP - php

<?php
$response = '{
"CCRResponse": {
"Status": "1",
"CIRReportDataLst": [
{
"CIRReportData": {
"IDAndContactInfo": {
"PersonalInfo": {
"Name": {
"FullName": "URMILA GUPTA ",
"FirstName": "URMILA ",
"LastName": "GUPTA "
},
" AliasName": {},
"DateOfBirth": "1951-11-05"
}
}
}
}
]
}
}';
$r1 = json_encode(json_decode($response), JSON_PRETTY_PRINT);
$obj = json_decode($response, true);
foreach ($obj["CCRResponse"]["CIRReportDataLst"] as $key) {
echo $fullname = $key["CIRReportData"]["IDAndContactInfo"]["PersonalInfo"]["Name"]
["FullName"];
}?>
Can any help me why I am getting
Notice: Undefined index: CIRReportData error?
I am able to get a response from my code but still getting errors.
You can find the full code here. Stack overflow doesn't all me to post full
https://app.box.com/s/sxt0eygha3ltx9bc59u0kn0kf0qqfzcm

foreach ($obj["CCRResponse"]["CIRReportDataLst"] as $key) {
if (isset($key["CIRReportData"]["IDAndContactInfo"]["PersonalInfo"]["Name"]["FullName"])) {
echo $key["CIRReportData"]["IDAndContactInfo"]["PersonalInfo"]["Name"]["FullName"];
}
}
It worked for me.

Related

Group json nested object in php

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"}
]}

How to access array inside document using PHP [duplicate]

This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 6 years ago.
I want print specific vars from array inside a document.
JSON structure:
{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
},
etc
]
}
}
I am trying to do this in PHP (and already decoded that json). How can I access and print all ids using foreach or for?
I can not understand how I can manage "return" scope.
You can decode the json contents and use it like an array. This should work:
$json = json_decode($string, true);
$contacts = $json['return']['contacts'];
foreach($contacts as $contact){
echo $contact['contact']['id'];
}
Json decode to array:
$json = json_decode($strjson, true); // decode the JSON into an associative array
and
echo "<pre>";
print_r($json);
And foreach:
foreach ($json as $key => $value) {
echo $key . " ". $value. "<br>";
}
Working example:
$json = '{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
}
]
}
}';
$json = json_decode($json, true);
print_r($json);
foreach ($json['return']['contacts'] as $val) {
echo $val['contact']['id']."<br>";
}
Try this code:
$json ='
{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
},
etc
]
}
}
';
$array = json_decode($json,TRUE);
foreach($array['return']['contacts'] as $value ){
echo $value['id'];
}
Use file_get_contents function if you want to pull data from file.

Select individual column json in php

{
"responseData": {
"results": [
{
"title": "sobig",
"titleNoFormatting": "test",
},
{
"title": "test 2 ",
"titleNoFormatting": "test 2sd",
},
{
"title": "asdasdasda",
"titleNoFormatting": "asdasdasd",
},
{
"title": "A Warming",
"titleNoFormatting": "A Warming",
}
.
.
.
.
{
"title": "last thing",
"titleNoFormatting": "sada",
}
],
I have json files like this.
for($i=$veri1; $i <= $veri2; $i++) {
$uri = "http://test.com/json/".$i."/0";
$json = json_decode(file_get_contents($uri));
if($json->data->price >= $nakit && $json->data->odds >= $oran)
{
I'm getting some data with this code correctly from another json file.
i want get data from first json code, if "title" == "sobig" . How can I do that.
$json->responseData->results->title == sobig is not working. How can I get data if title is sobig
$json= json_decode($response, true);
foreach ($json['responseData']['results'] as $key => $value) {
if ($value == 'sobig') {
// found it
}
}
Try this example to see if this may fix your issue.
<?php
$json = '{ "responseData": {
"result" : [
{ "title": "sobig" , "titleNo":"test"},
{ "title": "loco" , "titleNo":"test"},
{ "title": "tom" , "titleNo":"test"}
]
}}';
$jsonDecoded = json_decode($json);
foreach ($jsonDecoded->responseData->result as $key => $value) {
var_dump($value); echo '<br>';
if($value->title == 'sobig'){
echo "we did it!!";
echo "<br>";
}
}
?>
I place a couple of var dumps so you can see the stucture of your object and why you need to use the foreach

Can't deal with foreach levels with JSON decoding

I've been dealing with this problem around 5 hours, so I think that's time to ask here.
I'm retrieving data using Facebook Graph API and using JSON decoding to put it all together on a PHP.
Here's FB Graph:
{
"feed": {
"data": [
{
"message": "A file.",
"id": "831407506978898_831408573645458",
"attachments": {
"data": [
{
"target": {
"id": "1041214692589250",
"url": "https://www.facebook.com/download/A-PDF-FILE.pdf"
},
"title": "Clase 01 - Vías de administración.pdf",
"type": "file_upload",
"url": "https://www.facebook.com/download/A-PDF-FILE.pdf"
}
]
}
},
{
"picture": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"message": "A photo.",
"id": "831407506978898_831408496978799",
"attachments": {
"data": [
{
"description": "A photo.",
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"width": 720
}
},
"target": {
"id": "10207838160017396",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
},
"type": "photo",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
}
]
}
},
{
"picture": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"id": "831407506978898_831408450312137",
"attachments": {
"data": [
{
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"width": 720
}
},
"target": {
"id": "10207838168217601",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
},
"type": "photo",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
}
]
}
},
{
"message": "TEST",
"id": "831407506978898_831407576978891"
},
{
"id": "831407506978898_831407516978897"
}
],
"paging": {
"previous": "https://graph.facebook.com/...alotofjunk"
}
},
"id": "0000000000000"
}
And my PHP is the following one:
<?php
header('Content-Type: text/html; charset=utf-8');
$limit = 60; // The number of posts fetched
$access_token='TOKEN NUMBER';
$group_id = 'GROUPNUMBER';
$url1 = 'https://graph.facebook.com/'.$group_id.'?access_token='.$access_token;
$des = json_decode(file_get_contents($url1)) ;
$url2 = "https://graph.facebook.com/{$group_id}/feed?access_token={$access_token}";
$data = json_decode(file_get_contents($url2));
?>
<?
$counter = 0;
foreach($data->data as $d) {
if($counter==$limit)
break;
?>
<? $themessage = (isset($d->message) ? $d->message : false); ?>
<? print $themessage ?>
<? $thepicture = (isset($d->picture) ? $d->picture : false); ?>
<? print "<img src=\"$thepicture\">" ?>
<!--THE PROBLEM IS FROM HERE.... -->
<?
$counter = 0;
foreach($d->attachments->data as $d2) {
if($counter==$limit)
break;
?>
<? $attachments = (isset($d2->url) ? $d2->url : false); ?>
<? print $attachments ?>
<?
}
?>
<!-- ...TO HERE -->
<?
$counter++;
}
?>
I get a perfect output of $themessage and $thepicture, but I with $attachments I receive the following errors:
Notice: Undefined property: stdClass::$attachments in...
Notice: Trying to get property of non-object in...
Warning: Invalid argument supplied for foreach() in...
I've already read this: Trouble with Facebook multi-level json php foreach loop, but no luck.
How can I fix this?. Thanks a lot!
You need to be careful when you're chaining objects - especially in loops. One empty object will bring down the whole show. Try this:
$counter = 0;
if( isset( $d->attachments ) )
{
foreach( $d->attachments->data as $d2 )
{
....
}
}
Notice: Undefined property: stdClass::$attachments in...
Notice: Trying to get property of non-object in...
Warning: Invalid argument supplied for foreach() in...
You're getting this error becoz you're missing feed object in your first loop
HereDEMO
Replace
foreach($data->data as $d) {
if($counter==$limit)
break;
?>
With
foreach($data->feed->data as $d) {
if($counter==$limit)
break;
?>
Your JSON is in this format paste your JSON here JSON Format Viewer and check it
I tried your code i'm able to get url printed HereDEMO
$data="Your JSON Here"
foreach($data->feed->data as $d) {
$themessage = (isset($d->message) ? $d->message : false);
print("\n".$themessage);
$thepicture = (isset($d->picture) ? $d->picture : false);
print("\n<img src='$thepicture'>");
foreach($d->attachments->data as $d2) {
$attachments = (isset($d2->url) ? $d2->url : false);
print("\n".$attachments);
}
}
Side Note: you're initailising $counter = 0; twice once inside loop
and outside the loop its bad Even for $attachment, Its my opinion after looking your code for first time whatever you're reason be behind it

How to access multi-level json tree in PHP for database insertion

I am accessing an API where the JSON data is returned as below.
{
"name": "",
"count": 4,
"frequency": "",
"version": 3,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"thisversionrun": "",
"results": {
"collection": [{
"textlink": {
"href": "http://text1.com",
"text": "Text 1"
},
"index": 1,
"url": ""
}, {
"textlink": {
"href": "http://text2.com",
"text": "Text 2"
},
"index": 2,
"url": ""
}, {
"textlink": {
"href": "http://text3.com",
"text": "Text 3"
},
"index": 3,
"url": ""
}, {
"textlink": {
"href": "http://text4.com",
"text": "Text 4"
},
"index": 4,
"url": ""
}]
}}
As you can see the JSON tree returned has multi-step levels. I am wanting to be able to take some of the deeper levels, in PHP, and insert into a database.
Currently I am using this code to try and echo the data (once I am able to I can then work on inserting it to a database no problem)
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);
foreach($results as $item) {
echo $item->results[0]->collection[0]->textlink[0]->href;
echo "<br>";
echo $item->results->collection['href'];
echo "<br>";
echo $item->results->collection['text'];
}
?>
As you can see above I have tried several ways to access the deeper levels f data that are being displayed but with no avail.
I am currently getting errors of 'trying to get property of a non-object'. How is it possible to reach the data in this array?
try:
echo $results['results']['collection'][0]['textlink']['href'];
$obj = json_decode( $json, true ); foreach ( $obj['key'] as $key => $value ) { echo $value; }
foreach ($response['results']['collection'] as $textlink) {
$row = $textlink['textlink'];
echo $row['href'];
echo "<br>";
echo $row['text'];
echo "<br>";
}
you can do foreach like this, which loops only items in collection
I would suggest to do something like this (According to me, correct way to fetch results from API responses):
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$response = json_decode($response);
foreach($response->results->collection as $item) {
echo $item->textlink->href;
echo "<br>";
echo $item->textlink->text;
echo "<br>";
echo $item->index;
echo "<br>";
echo $item->url;
}
?>

Categories