数组错误-无效参数- PHP


Array error - Invalid argument - PHP

变量$x:

object(stdClass)#3 (5) { 
     ["polarity"]=> string(7) "neutral"
     ["subjectivity"]=> string(9) "objective" ["text"]=> string(0) ""
     ["polarity_confidence"]=> float(0.77262067409886)
     ["subjectivity_confidence"]=> float(0.54271560213894) 
}

以上是$x从var_dump的输出。

我正在尝试获得极性值("中性")。

我尝试了以下操作,但得到警告:为foreach()提供了无效参数

foreach($x->polarity as $polarity) {
    $value = $x->polarity[0];
    var_dump ($value);
}

如上所述,

  $x->polarity;

试试这个:

foreach($x as $key => $value) {
    print "$key => $value'n";
}
echo "'n";