【问题标题】:Fatal error: cannot use stdClass as array致命错误:不能使用 stdClass 作为数组
【发布时间】:2013-09-27 11:22:50
【问题描述】:

我从一位前同事那里继承了一些代码,由于它最近已投入生产,因此给我们带来了一些问题。我不是 php 开发人员,如果这看起来含糊不清,我深表歉意,但它已经落到我头上了(不要问!)。

我们正在调用函数getLoan() 来从现有应用程序中预填充应用程序表单。这应该返回一个数组,但我收到 stdClass 错误。

$result = getLoan(); //this is the line that is erroring
if (isset($result->GetResult->Debtor->Addresses->Address[0])) $current_address = clone     $result->GetResult->Debtor->Addresses->Address[0];
else $current_address = clone $result->GetResult->Debtor->Addresses->Address;

if ($result === false)
{
  echo("No LID given");
  die;
}
$attr = 'selected';

和功能:

function getLoan() {
//globals
/*
global $client;
global $test;
global $result;
*/
global $thisurl;

if (isset($_GET['LID'])) 
         $pLoanID = $_GET['LID'];
else 
         return false;
$test = array(
        "pLoanID" => $pLoanID,
);

//print_r($Address); //print address object as a test
//send to gateway starts**********
$url = "https://www.blahblah.asmx?WSDL";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));

try {
    $result = $client->Get($test);
}
catch(Exception $e) {
    //header("Location: http://" . $_SERVER['REMOTE_ADDR'] . "/application-form-new");
    print_r($e);
    exit;
}

$thisurl = "continue-app?LID=" . $pLoanID;
if (isset($result)) return $result;
else return false;
}

如何将函数返回的值作为数组赋值给 $result?

非常感谢

【问题讨论】:

  • 你说 getLoan() 应该返回一个数组,但你在它的调用 $result->GetResult 下将它用作对象
  • 使用类型转换,如 (array)$result
  • 好的,在这种情况下我需要将其转换为对象吗?

标签: php stdclass


【解决方案1】:
array = json_decode(json_encode(object),true);

【讨论】:

  • $result = json_decode(json_encode(getLoan(),true)); 期望参数 1 是字符串
  • json_decode() 如果您将 true 作为第二个参数传递,则返回数组。基本上,您将 json 编码对象传递给 json_decode,并将 true 作为第二个参数,它将返回该对象的数组。这就是我在数组和对象之间交换的方式
  • @ajguk 你做错了请检查函数调用的格式并打开和关闭大括号
  • $result = json_decode(json_decode(getLoan()),true); - 同样的错误
  • 我很抱歉。我现在得到“试图获取非对象的属性” - for $result
猜你喜欢
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2014-07-18
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多