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);
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:
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.
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'];":
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.