猿问

为什么我会收到不支持的操作数错误 (PHP)?

我正在测试一个新功能,突然开始收到不支持的操作数类型错误消息。捕获错误的代码不是我写的,所以我无法更改它。我完全不知道为什么我会收到错误消息。


错误说它是在线第 76 行,即


$transcripts[$inmateshort] = $job->getTranscript() + array(

    'inmatename' => $inmateshort,

    'inmatelong' => $inmatelong,

    'parentid' => $job->parentid

);

if 的 else 部分。


这是更多代码:


我在这里调用函数:


echo '<a href="' . site_url('invoice/copyTranscriptsToParent/' . $job['id']) . '" id="copyCreateInvoice">Copy transcripts to parent job</a>';

这是功能:


function copyTranscriptsToParent($jobs) {

    $transcripts = array();

    foreach ($jobs as $job) {

        $name = explode(' ', strtok($job->re, ';'), 2); 

        $nameshort = $name[1] . ', ' . substr($name[0], 0, 1); 

        $namelong = $name[1] . ', ' . $name[0]; 

        if (array_key_exists($nameshort, $transcripts)) {

            $transcripts[$nameshort]['personname'] = $transcripts[$nameshort]['namelong']; 

            $transcripts[$namelong] = $job->getTranscript() + array('personname' => $namelong, 'parentid' => $job->parentid);

        } else {

            $transcripts[$nameshort] = $job->getTranscript() + array('personname' => $nameshort, 'namelong' => $namelong, 'parentid' => $job->parentid);

        } 

    }

    $files = array();

    foreach ($transcripts as $transcript) {

        $origfilename = strtoupper($transcript['personname']);

        $filename = $origfilename;

        $i = 1;

        while (in_array($filename, $files)) {

            $i++;

            $filename = $origfilename . ' (' . $i . ')';

        }


正如我所说,我无法更改此功能,为什么会出现错误?任何帮助深表感谢。


郎朗坤
浏览 137回答 2
2回答

慕哥9229398

从您的评论中,我们发现$job->getTranscript()返回NULL.&nbsp;您不能将数组添加到NULL值,这将导致以下错误。致命错误:不支持的操作数类型由于您已经声明您不能更改您的copyTranscriptsToParent()函数,您可以编辑您的getTranscript()方法以在失败时返回一个空数组而不是 null,以便您始终将一个数组添加到一个数组中。或者,您可以确保传递给copyTranscriptsToParent()函数 (the&nbsp;$jobs) 的任何内容都具有getTranscript()不返回 null 的有效调用。

当年话下

您可以尝试使其无错误,如下所示:-if( !empty($job->getTranscript()) ) {&nbsp; &nbsp; &nbsp;if( gettype($job->getTranscript()) == "object") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//convert to array and store it to a variable&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;$getTranscript_data = $job->getTranscript();} else {&nbsp; &nbsp; &nbsp;$getTranscript_data = array();}$transcripts[$inmateshort] = array_merge($getTranscript_data + array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'inmatename' => $inmateshort,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'inmatelong' => $inmatelong,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'parentid' => $job->parentid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;));
随时随地看视频慕课网APP
我要回答