Php 无法将stdClass类型的对象用作数组?
我使用Php 无法将stdClass类型的对象用作数组?,php,json,Php,Json,我使用json\u decode()时遇到一个奇怪的错误。它可以正确解码数据(我使用print\r看到它),但当我尝试访问阵列中的信息时,我得到: Fatal error: Cannot use object of type stdClass as array in C:\Users\Dail\software\abs.php on line 108 我只试过这样做:$result['context']其中$result包含由json\u decode()返回的数据 如何读取此数组中的值?它不
json\u decode()
时遇到一个奇怪的错误。它可以正确解码数据(我使用print\r
看到它),但当我尝试访问阵列中的信息时,我得到:
Fatal error: Cannot use object of type stdClass as array in
C:\Users\Dail\software\abs.php on line 108
我只试过这样做:$result['context']
其中$result
包含由json\u decode()返回的数据
如何读取此数组中的值?它不是数组,而是stdClass类型的对象
您可以这样访问它:
echo $oResult->context;
var_dump($result->context);
$result = json_decode($json, true);
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
<!--Main content-->
<div id="mainholder"> <!-- div so that page footer can have a minum height from the
header -->
<h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
<br>
<br>
<article>
<h2></h2>
</article>
<?php
if (isset($servicecalls)) {
if (count ($servicecalls) > 0){
foreach ($servicecalls as $servicecall) {
echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
.$servicecall->ServiceCallDescription .'</a>';
}
}else echo 'No service Calls';
}
?>
<a href="/servicecalls/?new=true">Raise New Service Call</a>
</div> <!-- Main content end-->
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>
更多信息:使用true
作为json\u解码的第二个参数。这将把json解码成一个关联数组,而不是stdObject
实例:
$my_array = json_decode($my_json, true);
有关详细信息,请参阅。使用的第二个参数使其返回数组:
$result = json_decode($data, true);
$result = (array) json_decode($json);
默认情况下,该函数返回一个对象
您可以通过以下方式访问数据:
echo $oResult->context;
var_dump($result->context);
$result = json_decode($json, true);
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
<!--Main content-->
<div id="mainholder"> <!-- div so that page footer can have a minum height from the
header -->
<h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
<br>
<br>
<article>
<h2></h2>
</article>
<?php
if (isset($servicecalls)) {
if (count ($servicecalls) > 0){
foreach ($servicecalls as $servicecall) {
echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
.$servicecall->ServiceCallDescription .'</a>';
}
}else echo 'No service Calls';
}
?>
<a href="/servicecalls/?new=true">Raise New Service Call</a>
</div> <!-- Main content end-->
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>
如果您有类似于from date
(使用上述方法时,连字符将导致PHP错误)的标识符,则必须编写:
var_dump($result->{'from-date'});
如果需要阵列,可以执行以下操作:
echo $oResult->context;
var_dump($result->context);
$result = json_decode($json, true);
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
<!--Main content-->
<div id="mainholder"> <!-- div so that page footer can have a minum height from the
header -->
<h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
<br>
<br>
<article>
<h2></h2>
</article>
<?php
if (isset($servicecalls)) {
if (count ($servicecalls) > 0){
foreach ($servicecalls as $servicecall) {
echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
.$servicecall->ServiceCallDescription .'</a>';
}
}else echo 'No service Calls';
}
?>
<a href="/servicecalls/?new=true">Raise New Service Call</a>
</div> <!-- Main content end-->
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>
或将对象强制转换为数组:
$result = json_decode($data, true);
$result = (array) json_decode($json);
不使用括号,而是使用对象运算符。例如,基于数据库对象的数组在名为DB的类中创建如下:
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct() {
try{
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );
} catch(PDOException $e) {
$this->_error = true;
$newsMessage = 'Sorry. Database is off line';
$pagetitle = 'Teknikal Tim - Database Error';
$pagedescription = 'Teknikal Tim Database Error page';
include_once 'dbdown.html.php';
exit;
}
$headerinc = 'header.html.php';
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else{
$this->_error = true;
}
return $this;
}
public function action($action, $table, $where = array()) {
if(count($where) ===3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} = ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
public function results() {
return $this->_results;
}
public function first() {
return $this->_results[0];
}
public function count() {
return $this->_count;
}
}
classdb{
私有静态$\u实例=null;
私人$(pdo),
$\u查询,
$\u错误=false,
$\u结果,
$\计数=0;
私有函数_u构造(){
试一试{
$this->_pdo=new pdo('mysql:host='.Config::get('mysql/host')。;dbname='.Config::get('mysql/db')、Config::get('mysql/username')、Config::get('mysql/password');
}捕获(PDO$e){
$this->_error=true;
$newsMessage='对不起,数据库离线';
$pagetitle='Teknikal Tim-数据库错误';
$pagedescription='Teknikal Tim数据库错误页';
包括“dbdown.html.php”;
出口
}
$headerinc='header.html.php';
}
公共静态函数getInstance(){
if(!isset(self:$\实例)){
self::$_instance=new DB();
}
返回self::$\u实例;
}
公共函数查询($sql,$params=array()){
$this->_error=false;
如果($this->_query=$this->_pdo->prepare($sql)){
$x=1;
如果(计数($params)){
foreach($params作为$param){
$this->_query->bindValue($x,$param);
$x++;
}
}
}
如果($this->\u query->execute()){
$this->\u results=$this->\u query->fetchAll(PDO::FETCH\u OBJ);
$this->_count=$this->_query->rowCount();
}
否则{
$this->_error=true;
}
退还$this;
}
公共函数action($action,$table,$where=array()){
如果(计数($where)==3){
$operators=array(“=”、“>”、“=”、“您必须使用->
访问它,因为它是一个对象
将代码更改为:
$result['context'];
致:
您可以将stdClass对象转换为数组,如下所示:
$array = (array)$stdClass;
以下是函数签名:
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
当param为false(默认值)时,它将返回一个适当的php类型
当param为true时,它将返回关联数组
出现错误时,它将返回NULL
如果要通过数组获取值,请将assoc设置为true。今天也有相同的问题,解决方法如下:
echo $oResult->context;
var_dump($result->context);
$result = json_decode($json, true);
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
<!--Main content-->
<div id="mainholder"> <!-- div so that page footer can have a minum height from the
header -->
<h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
<br>
<br>
<article>
<h2></h2>
</article>
<?php
if (isset($servicecalls)) {
if (count ($servicecalls) > 0){
foreach ($servicecalls as $servicecall) {
echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
.$servicecall->ServiceCallDescription .'</a>';
}
}else echo 'No service Calls';
}
?>
<a href="/servicecalls/?new=true">Raise New Service Call</a>
</div> <!-- Main content end-->
<?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>
如果你调用json\u decode($somestring)
你会得到一个对象,你需要像$Object->key
那样访问它,但是如果你调用json\u decode($somestring,true)
你会得到一个字典,你可以像$array['key']
一样访问它,就像Php手册所说的那样
打印\u r-打印有关变量的可读信息
当我们使用json_decode();
时,我们得到一个stdClass类型的对象作为返回类型。
要在print\r()
内部传递的参数应该是数组或字符串。因此,我们不能在print\r()
内部传递对象。我找到了两种方法来处理这个问题
将对象强制转换为数组。
这可以通过以下方式实现
$a = (array)$object;
通过访问对象的键
如前所述,当您使用json_decode();
函数时,它返回一个stdClass对象。您可以在->
操作符的帮助下访问该对象的元素
$value = $object->key;
第一,如果对象具有嵌套数组,还可以使用多个键提取子元素
$value = $object->key1->key2->key3...;
它们是print_r()
的其他选项,如var_dump();
和var_export();
p.S:另外,如果将json_decode();
的第二个参数设置为true
,它将自动将对象转换为数组();
以下是一些参考资料:
当您试图以$result['context']
的形式访问它时,您将其视为一个数组,错误是它告诉您您实际上正在处理一个对象,那么您应该以$result->context的形式访问它
$results->fetch_array()
我之所以突然出现这个错误,是因为我的facebook登录突然停止工作(我也更换了主机)并抛出了这个错误。修复非常简单
问题就在这段代码中
$response = (new FacebookRequest(
FacebookSession::newAppSession($this->appId, $this->appSecret),
'GET',
'/oauth/access_token',
$params
))->execute()->getResponse(true);
if (isset($response['access_token'])) { <---- this line gave error
return new FacebookSession($response['access_token']);
}
注意第一行中的use off array()量词。要从json字符串获取数组结果,应将第二个参数设置为布尔值true
$result = json_decode($json_string, true);
$context = $result['context'];
否则$result将是一个标准对象。但您可以作为对象访问值
$result = json_decode($json_string);
$context = $result->context;
有时在使用API时,您只想将对象保留为对象。要访问具有嵌套对象的对象,可以执行以下操作:
我们假设当您打印对象时,您可能会看到:
print_r($response);
stdClass object
(
[status] => success
[message] => Some message from the data
[0] => stdClass object
(
[first] => Robert
[last] => Saylor
[title] => Symfony Developer
)
[1] => stdClass object
(
[country] => USA
)
)
要访问对象的第一部分,请执行以下操作:
print $response->{'status'};
这将产生“成功”
现在让我们为其他部分设置关键帧