尝试获取非对象的属性“月”,即使值存在

时间:2019-11-17 14:04:20

标签: php

我目前正在解码JSON文件的一部分,但是尽管print_r和var_dump都表明该值存在,但是却出现“试图获取非对象的属性” month”错误。

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);

}

错误:

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

似乎只有在'review'键之后才会失败,因为其他值肯定存在,并显示在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"
}

如果有人有任何想法,将不胜感激。

2 个答案:

答案 0 :(得分:3)

您的代码中有

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

因此,在尝试获取$month变量的那一行中,您将变量$review重新分配给其他名称,因此只需将此字段名称更改为其他名称,例如...

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

和其他任何参考文献

答案 1 :(得分:-1)

我使用的是数组而不是对象...无法对数组执行 $someData->year。您必须改用 $someData['year']。

对于某些人来说,这可能是 1+1=2,但我自己花了一些时间才弄明白:) 也许这也可以帮助其他人:P