1

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}
E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 1
    Can you please [edit] your question to show us a sample of JSON that decodes to the structure that causes trouble? – O. Jones May 16 '22 at 22:04
  • @O.Jones thanks for looking. Added the JSON. – Steve Weigold May 16 '22 at 22:11
  • When your object contains indexed arrays, it _should_ indicate that there could be multiple items within that property. This indicates that a loop is appropriate. https://3v4l.org/kRNeZ If there can never be more than one item in the subarray, then it is suboptimal data structure design to create the extra level of complexity. – mickmackusa Jun 24 '22 at 05:18

3 Answers3

1

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

Crimin4L
  • 610
  • 2
  • 8
  • 23
  • Sh*t show aside, I like your code. At issue though is I have an array OF stdClass objects. I need to get to something I can access the object by index, so a more extensive version of array_to_stdClass that gets me to $obj->0->desiredproperty and $obj->1->desiredproperty etc would get me there. I'll noodle on that unless you have another code snippet handy. – Steve Weigold May 17 '22 at 02:03
  • Why write 2 recursive functions to do what `json_decode(json_encode())` does? – mickmackusa May 17 '22 at 10:58
  • because if you knew anything about PHP, you'd know that is not what `json_decode(json_encode())` does. Not surprised though, you are the same person who asked me `Why pass zeros when your function definition expects bools?` https://onlinephp.io/c/4b183 – Crimin4L May 17 '22 at 20:54
  • https://3v4l.org/6nmlM Note, there is also no reason to modify by reference in your custom functions. – mickmackusa Jun 24 '22 at 05:02
1

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

0

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.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • I realize squarefeet isn't a member. It was simply a representative example of many possible members. And building is in the JSON. If it isn't included in the object its due an error in the decode. The start of the building array can be seen in the JSON in the area of "rea"}],"building":[{"airConditioning":"Central","foundation":null,"architecturalStyle":"Ranch/Rambler","yearBuilt":1965," – Steve Weigold May 17 '22 at 01:11
  • Further, I know foreach works. I said so in my question. I don't have the luxury of easily using it or I wouldn't have asked the question. I need the building properties for one or more buildings directly accessible in something indexed. I can "foreach" them into an array, but that didn't seem to work either. – Steve Weigold May 17 '22 at 01:13
  • If "$ResponseInfo->building[0]->airConditioning;" (modified to use a member that's there) doesn't navigate explicitly to the data I need, what does? Would you elaborate on that please? – Steve Weigold May 17 '22 at 01:35
  • Thanks for the edit! Unfortunately, it's the building[0] part of the reference that's generating the Undefined offset 0, even though it shouldn't be. The same code on test sites with cut and paste json data works. It's crazy. I've modified the routine and reduced it to the bare minimum to duplicate the problem and it's come down to a json_decode issue as near as I can tell. If I feed json_decode with the actual response it fails. If I feed json_decode with the same variable loaded with cut and paste JSON from the response, it works. – Steve Weigold May 17 '22 at 18:34
  • This question's gotten so bloated, I'm not sure how to proceed in terms of adding what I've found. The key code is: $ResponseInfo = json_decode($ResponseBody); If $ResponseBody is a string created with the cut and paste JSON, it works. If it's populated with $ResponseBody = $HTTPResponse->body(); from the HTML client, it fails. In both cases, the $ResponseBody string is identical to the best of my ability to test it. Same content, same length, same character encoding. Don't know what to check next. – Steve Weigold May 17 '22 at 18:42
  • The Stack-Overflowish way to move forward is to ask another question. From your most recent comment it looks like you use some code to generate this `$HTTPResponse` object. In your new question, show us that code, including the JSON decode and the attempt to access stuff in the JSON object. – O. Jones May 18 '22 at 11:27