Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trifek's avatar

Error after update php 7.3 to 8.1: Cannot access offset of type string on string

I have to "fix" a script that started to crash probably after changing php to 8.1. The problem is that I don't really have the ability to debug it (I found the error in the logs).

Error message:


Cannot access offset of type string on string#0 /home/www/classs.php(529): ClassName->convertXmlToExportXml()
#1 /home/www/classs.php(656): ClassName->sendToApi()
#2 /home/www/classs.php(103): ClassName->sendToApi2()
#3 {main}

The code looks like this:


$o = new TQuery();
$o->sql = "select * from tablename where id = " . $id;
$o->Open();
if (!$o->isRow) {
  return false;
}

$rCode = $o->fieldByName('r_code');
if ($rCode == 'AM' || $rCode == 'AG') {
    $rCode = 'A1';
} else {
    $rCode = 'A2';
}

    $parsedXml = $this->convertXmlToExportXml($rCode);
    if (!$parsedXml) {
        return false;
    }

private function convertXmlToExportXml( $rCode) {
.......
}

Line 529 is: $parsedXml = $this->convertXmlToExportXml($rCode);

Can anyone see a error?

Thank you in advance for your help :)

0 likes
5 replies
LaryAI's avatar
Level 58

The error message "Cannot access offset of type string on string" suggests that the code is trying to access an array offset on a string variable. This is likely due to a change in behavior between PHP 7.3 and 8.1.

One possible cause of this error is that the $rCode variable is not being set to a string value. To fix this, you can explicitly cast $rCode to a string before passing it to the convertXmlToExportXml method:

$parsedXml = $this->convertXmlToExportXml((string) $rCode);

Alternatively, you can modify the convertXmlToExportXml method to handle non-string values:

private function convertXmlToExportXml($rCode) {
    $rCode = (string) $rCode; // cast to string if necessary
    // rest of the method
}

Either of these changes should resolve the error.

ramonrietdijk's avatar

The line $parsedXml = $this->convertXmlToExportXml($rCode); should not cause any errors by itself. Could you share the content of convertXmlToExportXml? I think that $rCode or another variable is inappropriately used as an array.

trifek's avatar

@ramonrietdijk

private function convertXmlToExportXml( $rCode)
    {
        $icomponent = null;

        foreach ($xiComponents->iComponent as $icomp) {
            if (
                strtotime($icomp->startDate) <= strtotime($this->cTime) &&
                strtotime($icomp->endDate) >= strtotime($this->cTime)
            ) {
                if (
                    $this->decodeR(
                        (string) $rCode,
                        (string) $icomp->symbol
                    )
                ) {
                    $icomponent = $icomp;
                    break;
                }
            }
        }
        
        ////...... long code, but without $rCode

        $ls33objXml =
            "<Object><objectId>1</objectId><objectExternalIds><externalId><externalSystem>PartenonEHM</externalSystem><IdName>PM</IdName><IdValue></Object>";

        
        }
        return $ls33objXml;
    }

    private function decodeR($csR, $partR) {

        $converter = array(
         'I29-01' => 'PG',
         // .....
        );
        if (isset($converter[$partR]) && (string) $converter[$partR] === (string) $csR) {
            return true;
        } else {
            return false;
        }
        }
plies's avatar

I'm receiving the same error on some of my code: TypeError: Cannot access offset of type string on string in nasm_core_preprocess_field() (line 68 of /code/web/modules/custom/nasm_core/nasm_core.module). This error occurs in PHP 8.1, but not PHP 8.2. But I need to get it to work in 8.1. This is in a Drupal 9 preprocess function. The full function is below. The line that is throwing the error is the one that reads "$variables['items']['0']['content']['#title']['#text'] = $variables['items']['0']['content']['#title']['#context']['value'];":

/**

  • Implements hook_preprocess_field(). */ function nasm_core_preprocess_field(&$variables, $hook) { $title_field_names = [ 'title', 'field_title_text', 'field_callout_title', 'field_section_title', 'field_display_title', 'field_subtitle', 'name', ];

if (in_array($variables['field_name'], $title_field_names)) { // Change the format of plain text title fields to 'filtered_html' so they // can render limited html tags. if ($variables['items']['0']['content']['#type'] === 'inline_template') { $variables['items']['0']['#formatter'] = 'text_default'; $variables['items']['0']['content']['#text'] = $variables['items']['0']['content']['#context']['value']; $variables['items']['0']['content']['#type'] = 'processed_text'; $variables['items']['0']['content']['#format'] = 'filtered_html'; unset($variables['items']['0']['content']['#template']); } elseif ($variables['items']['0']['content']['#type'] === 'link') { $variables['items']['0']['content']['#title']['#text'] = $variables['items']['0']['content']['#title']['#context']['value']; $variables['items']['0']['content']['#title']['#type'] = 'processed_text'; $variables['items']['0']['content']['#title']['#format'] = 'filtered_html'; unset($variables['items']['0']['content']['#title']['#template']); } } }

Any ideas? As any fellow Drupalists out there know, arrays to access fields are crazy long in Drupal.

Sorry this is one big glob of code - it looks nicely formatted in the preview, but I can't seem to get it to "stick" when I post.

Please or to participate in this conversation.