Skip to content
Advertisement

Trying to get property ‘month’ of non-object even though value exists

I am currently decoding part of a JSON file, however getting a ‘Trying to get property ‘month’ of non-object’ error, although print_r and var_dump both show that the value exists.

JSON:

{
    "totalRecords":57775,
    "start":1573672346,
    "rows":57775,
    "tableName":"tblReviews",
    "tableData":[{
        "reviewID":1,
        "markerID":10605,
        "userID":1,
        "review":"Test",
        "month":5,
        "year":2011,
        "price":9,
        "location":8,
        "facilities":0,
        "view":7,
        "unitTypeID":3,
        "reviewer":"admin",
        "profileID":3,
        "profileType":"Middle-age couple",
        "unitTypeDescription":"Medium Motorhome under 8m"
    }]
}

PHP:

$reviewsData = json_decode($json, false);

$reviews = $reviewsData->tableData;

foreach($reviews as $review) {

    $reviewID = (int)$review->reviewID;
    $markerID = (int)$review->markerID;
    $userID = (int)$review->userID;
    $review = (string)$review->review;
    $month = (int)$review->month;
    $year = (int)$review->year;
    $price = (int)$review->price;
    $location = (int)$review->location;
    $facilities = (int)$review->facilities;
    $view = (int)$review->view;
    $unitTypeID = (int)$review->unitTypeID;
    $reviewer = (string)$review->reviewer;
    $profileID = (int)$review->profileID;
    $profileType = (string)$review->profileType;
    $unitTypeDescription = (string)$review->unitTypeDescription;

    $db->insertReview($reviewID, $markerID, $userID, $review, $month, $year, $price, $location, $facilities, $view, $unitTypeID, $reviewer, $profileID, $profileType, $unitTypeDescription);

}

Error:

Notice: Trying to get property 'month' of non-object in processReviews.php on line 36
Notice: Trying to get property 'year' of non-object in processReviews.php on line 37
Notice: Trying to get property 'price' of non-object in processReviews.php on line 38
Notice: Trying to get property 'location' of non-object in processReviews.php on line 39
Notice: Trying to get property 'facilities' of non-object in processReviews.php on line 40
Notice: Trying to get property 'view' of non-object in processReviews.php on line 41
Notice: Trying to get property 'unitTypeID' of non-object in processReviews.php on line 42
Notice: Trying to get property 'reviewer' of non-object in processReviews.php on line 43
Notice: Trying to get property 'profileID' of non-object in processReviews.php on line 44
Notice: Trying to get property 'profileType' of non-object in processReviews.php on line 45
Notice: Trying to get property 'unitTypeDescription' of non-object in processReviews.php on line 46

It only seems to fail after the ‘review’ key, as the other values are definitely there, and are shown on var_dump:

array(15) {
  ["reviewID"]=>
  int(1)
  ["markerID"]=>
  int(10605)
  ["userID"]=>
  int(1)
  ["review"]=>
  string(4) "Test"
  ["month"]=>
  int(5)
  ["year"]=>
  int(2011)
  ["price"]=>
  int(9)
  ["location"]=>
  int(8)
  ["facilities"]=>
  int(0)
  ["view"]=>
  int(7)
  ["unitTypeID"]=>
  int(3)
  ["reviewer"]=>
  string(5) "admin"
  ["profileID"]=>
  int(3)
  ["profileType"]=>
  string(17) "Middle-age couple"
  ["unitTypeDescription"]=>
  string(25) "Medium Motorhome under 8m"
}

If anyone has any ideas it would be greatly appreciated.

Advertisement

Answer

In your code you have

$review = (string)$review->review;
$month = (int)$review->month;

so in the line before you try to get the $month variable, you reassign the variable $review to something else, so just change this field name to something else to something like…

$reviewValue = (string)$review->review;

and any other references

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement