skip to Main Content

The short version is that I have an array of objects which was decoded from a JSON response from a web service and despite my best efforts, I can’t access the objects in that array by index.

I’m specifically decoding the response as so:

$ResponseInfo = json_decode($ResponseBody, false);  //give to me as an object

I’m having no problems accessing the properties of the resulting object until I get to a property that is an array of objects. For context, it’s property information that includes details on one or more buildings.

I expected to be able to access details on the buildings by index such as:

$ac = $ResponseInfo->building[0]->airConditioning;

This does not work. I get an index 0 undefined. Oddly, array_keys gives me:

Array ( [0] => 0 )

So it seems like the index method should work, and it isn’t some strange key, but alas it doesn’t.

A print_r gives me this:

Array ( [0] => stdClass Object ( [airConditioning] => Central [foundation] => [architecturalStyle] => Ranch/Rambler [yearBuilt] => 1965 [fireplace] => [noOfUnits] => 1 [effectiveYearBuilt] => [sewer] => Municipal/Public [heating] => Central [class] => [comments] => [quarterBaths] => [totalStories] => 1 [kitchens] => 2 [water] => Municipal/Public [classDescription] => [quality] => C- [totalRooms] => 5 [bedrooms] => 3 [condition] => Average [baths] => [threeQuarterBaths] => [halfBaths] => [occupancyStatus] => [fullBaths] => 1 ) ) Array ( [0] => BuildingInformationObject Object ( [airConditioning] => Central [foundation] => [architecturalStyle] => Ranch/Rambler [yearBuilt] => 1965 [fireplace] => [noOfUnits] => 1 [sewer] => Municipal/Public [heating] => Central [totalStories] => 1 [kitchens] => 2 [water] => Municipal/Public [totalRooms] => 5 [bedrooms] => 3 [condition] => Average [halfBaths] => [occupancyStatus] => [fullBaths] => 1 ) )

I also tried assigning the array to a new variable, but I still get the index 0 undefined.

$BuildingInfo  = $ResponseInfo->building;

Oddly, what does work is:

foreach ($BuildingInfo as $ThisBuilding) {

In that fashion $ThisBuilding-airconditioning (or whatever property) works fine. So I even tried creating a brand new array and manually assigning the individual properties, as so:

$Buildings = array();
$i = 0;

foreach ($BuildingInfo as $ThisBuilding)
{
    $Buildings[$i] = new BuildingInformationObject();
    //$Buildings[$i] = $ThisBuilding;     //just assigning variable didn't work, so copied properties manually
    
    $Buildings[$i]->airConditioning    = $ThisBuilding->airConditioning;   
    $Buildings[$i]->foundation         = $ThisBuilding->foundation;        
    $Buildings[$i]->architecturalStyle = $ThisBuilding->architecturalStyle;

etc..

After ALL that, attempting $Buildings[0]->airConditioning still gives me the dreaded index 0 undefined, even on the new array.

I even tried something in the form of

echo $data->{'0'}->UserName;

Such as:

$BuildingInfo->{'0'}->airConditioning;

That I found here on SO. This method was apparently valid with old versions of PHP, but it’s not working now.

What in the world am I missing here? Thanks for any help.

Steve

Edit: I realize I could possibly change the json_decode to simply return an array and then deal with it as a multidimensional array, but I’m trying to avoid rewriting a bunch of code. I’m dealing ultimately with hundreds of object properties in the JSON response and the only ones I’m having issue with are the properties that are arrays of objects.

Edit 2: I’m loath to add this because of it’s size, but in response to a comment, here’s the JSON that I’m dealing with. I didn’t want to try to cut it down for fear of inadvertantly deleting whatever is causing my problem. If there’s a better way to share a bulk of unmolested JSON, please let me know.

Edit 3: in response to a comment that the building array isn’t in the JSON, I cut and pasted the below JSON from this site into https://jsonformatter.org/json-parser and looked at the JSON in tree view. The building array I need to access by index is in fact in this JSON.

{"success":true,"status":200,"bundle":[{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2016,"marketLandValue":26930,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2014,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2016,"marketTotalValue":89400,"taxID":"458467","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2016","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":62470,"totalValue":31290,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":9430,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":21860,"taxAmount":1701.78,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2016"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2008,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2008,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2008,"marketTotalValue":112120,"taxID":"320818","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2008","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82740,"totalValue":39242,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28959,"taxAmount":1143.66,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2008"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2005,"marketLandValue":24950,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2005,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2005,"marketTotalValue":107700,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2005","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82750,"totalValue":37696,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8733,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28963,"taxAmount":1631.24,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2005"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2002,"marketLandValue":23760,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2002,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2002,"marketTotalValue":102590,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2002","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":78830,"totalValue":35907,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8316,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":27591,"taxAmount":1352.82,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2002"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2011,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2011,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2011,"marketTotalValue":103050,"taxID":"60577","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2011","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":73670,"totalValue":36068,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":25785,"taxAmount":1591.38,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2011"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2010,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2010,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2010,"marketTotalValue":105520,"taxID":"475393","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2010","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":76140,"totalValue":36932,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":26649,"taxAmount":1159.18,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2010"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2007,"marketLandValue":24950,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2007,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2007,"marketTotalValue":107700,"taxID":"61840","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2007","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":82750,"totalValue":37696,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8733,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":28963,"taxAmount":1258.38,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2007"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2014,"marketLandValue":26930,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2014,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2013,"marketTotalValue":89400,"taxID":"121009","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2014","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":62470,"totalValue":31290,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":9430,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":21860,"taxAmount":1678.56,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2014"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2003,"marketLandValue":23760,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2003,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2003,"marketTotalValue":102590,"taxID":null,"taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2003","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":78830,"totalValue":35907,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":8316,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":27591,"taxAmount":1380.02,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2003"},{"landUseDescription":"Single Family Residential","zoningDescription":null,"year":2009,"marketLandValue":29380,"numberOfBuildings":1,"lotSizeIrregular":"85 X 138","county":"Butler County","pools":[],"censusTract":"390170118002005","areas":[{"areaSquareFeet":1492,"type":"Living Building Area"},{"areaSquareFeet":1144,"type":"Base Building Area"},{"areaSquareFeet":1140,"type":"Adjusted Building Area"},{"areaSquareFeet":1492,"type":"Zillow Calculated Finished Area"},{"areaSquareFeet":348,"type":"Addition Area"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965,"fireplace":null,"noOfUnits":1,"effectiveYearBuilt":null,"sewer":"Municipal/Public","heating":"Central","class":null,"comments":null,"quarterBaths":null,"totalStories":1,"kitchens":2,"water":"Municipal/Public","classDescription":null,"quality":"C-","totalRooms":5,"bedrooms":3,"condition":"Average","baths":null,"threeQuarterBaths":null,"halfBaths":null,"occupancyStatus":null,"fullBaths":1}],"lotSizeFrontage":null,"parcelID":"112166032","marketValueYear":2009,"lotTopography":"Rocky","ownerName":["ROBERT D SUTTON"],"taxYear":2009,"marketTotalValue":105520,"taxID":"478717","taxExemption":null,"legal":{"phase":null,"tract":null,"city":null,"municipality":null,"section":null,"lot":null,"otherLot":null,"unit":null,"lotDescription":"1141 ENT","district":null,"block":null,"subdivisionName":null,"township":null},"id":"112166032-2009","state":"OH","lotSizeDepth":null,"apn":"R8000-017.000-071","marketImprovementValue":76140,"totalValue":36932,"address":{"zip":"45067","city":"Trenton","zip4":"1117","streetPre":null,"house":"431","unitType":null,"unit":null,"street":"Kenwood","houseExt":null,"streetSuffix":"Ln","state":"OH","streetPost":null,"full":" 431 Kenwood Ln Trenton OH 45067"},"landUseCode":"RR101","landUseGeneral":"RR","coordinates":[-84.465074,39.489885],"zpid":"33237764","fips":"39017","landValue":10283,"zoningCode":null,"lotSizeAcres":0.27,"garages":[],"improvementValue":26649,"taxAmount":1154,"lotSizeSquareFeet":11761.2,"url":"api.bridgedataoutput.com/api/v2/pub/assessments/112166032-2009"}],"total":21}

3

Answers


  1. Chosen as BEST ANSWER

    Well. I'm an idiot. Thanks to everyone that took a look. I'm sorry I wasted your time.

    The correct method for accessing the data I need is in fact:

    $ResponseInfo->bundle[0]->building[0]->airConditioning;
    

    adjusted of course to the correct variables and members. The reason I was having problems is that I got tunnel vision and when I tried to reduce my code to the simplest elements, I didn't do so completely.

    In particular, the code I'm trying to debug is called repeatedly in a loop to pull property information and when I simplified things I forgot that. My test was pulling data for five properties and I got fixated on the first one, who's data was correct, and was verified to work repeatedly by me and others.

    The second property in the loop however, didn't play quite so nice. As it turns out, the JSON data being returned for that property is in fact missing the elements I needed. That of course is a completely different issue.

    Just because an error was thrown, doesn't necessarily mean it was the first iteration of the loop that caused it and I lost sight of that. The code I'm working on though is so modularized though that the loop that was my nemesis was quite removed from the routines I was focused on.

    I probably should have just deleted the question, but I figured those that took time to look at it would appreciate a follow up, and maybe it will help someone else think about the bigger picture as well when troubleshooting.

    Thanks again all. Steve


  2. Decoding JSON into php is a mulish operation: it’s a cross between a Javascript horse and a php donkey. So it can get a little confusing I think, if not stubborn.

    Here are two ways to add up the square footage buried in your JSON object.

    /* php objects */
    $ResponseInfo = json_decode( $json, false );
    $sqft = 0;
    foreach ( $ResponseInfo->bundle as $bundle ) {
      foreach ( $bundle->areas as $area ) {
        $sqft += $area->areaSquareFeet;
      }
    }
    
    /* php traditional associative arrays */
    $ResponseInfo = json_decode( $json, true );
    $sqft         = 0;
    foreach ( $ResponseInfo['bundle'] as $bundle ) {
      foreach ( $bundle['areas'] as $area ) {
        $sqft += $area['areaSquareFeet'];
      }
    }
    

    In either case when you start with the $ResponseInfo output of json_decode you must navigate explicitly from the top level of that object to the data you need. This sort of expression doesn’t do that.

    $sqft = $ResponseInfo->building[0]->squarefeet;
    

    building isn’t a member of that decoded object, and there’s no member called squarefeet in your JSON.

    Edit

    This expression accesses your first building object in the first bundle object in your JSON (when decoded to a php object).

    $ResponseInfo->bundle[0]->building[0]->airConditioning;
    

    As you know, the payload of your API’s response is in an array called bundle. (The other stuff in your top-level response is the API wrapper showing request status.) So the above expression looks at the first element of the bundle array to find the building array. Then it looks at the first element of the building array to find the object property called noOfUnits.

    I should explain what I did — how I figured out my answer. I have notepad++ with the JSON formatter plugin enabled. I put your JSON into a scratch document in that editor and formatted it. Then it became easy to explore your JSON object level by level.

    php objects have the unfortunate property that a reference to $object->undefined returns null instead of throwing an error. And a foreach() on a null just loops zero times.

    Login or Signup to reply.
  3. It seems like you are using stdClass Object which may be throwing you off.

    If you want to convert the data fully into an stdClass Object or fully into an array for easier manageability; you can use either one of the functions below:

    function array_to_stdClass(&$array){
        $tmp = $array;
        $array = new stdClass();
        foreach($tmp as $k => $v) $array->$k = (!is_object($v) && !is_array($v)) ? $v:array_to_stdClass($v);
        return $array;
    }
    
    function stdClass_to_array(&$stdClass){
        $tmp = $stdClass;
        $stdClass = array();
        foreach($tmp as $k => $v) $stdClass[$k] = (!is_object($v) && !is_array($v)) ? $v:stdClass_to_array($v);
        return $stdClass;
    }
    

    Live Demo/Example here: https://onlinephp.io/c/2d00a

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search