# Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /var/www/app/src/Utils/Parser.php:23

- **ID:** `php/type-error-array-to-string-conversion`
- **Domain:** php
- **Category:** type_error
- **Error Code:** `E_ERROR`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

PHP 8+ throws a TypeError when code attempts to use string offset access on a variable that is actually a string, often because an array was expected but a scalar string was passed due to incorrect data parsing or API response handling.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| PHP 8.0 | active | — | — |
| PHP 8.1 | active | — | — |
| PHP 8.2 | active | — | — |
| PHP 8.3 | active | — | — |

## Workarounds

1. **Add strict type validation before accessing offsets: `if (is_array($data) && isset($data['key'])) { $value = $data['key']; } else { $value = ''; }`. Also ensure the data source (e.g., json_decode) is called with `true` second argument for associative arrays.** (90% success)
   ```
   Add strict type validation before accessing offsets: `if (is_array($data) && isset($data['key'])) { $value = $data['key']; } else { $value = ''; }`. Also ensure the data source (e.g., json_decode) is called with `true` second argument for associative arrays.
   ```
2. **Use a helper function to safely extract values: `function safeGet($data, $key, $default = null) { return is_array($data) ? ($data[$key] ?? $default) : $default; }` and replace all direct array accesses with this function.** (85% success)
   ```
   Use a helper function to safely extract values: `function safeGet($data, $key, $default = null) { return is_array($data) ? ($data[$key] ?? $default) : $default; }` and replace all direct array accesses with this function.
   ```

## Dead Ends

- **** — The suppression operator only prevents error display; the underlying type mismatch still causes incorrect behavior, and subsequent code may fail unpredictably. (95% fail)
- **** — While it prevents the crash, it does not address the root cause (e.g., malformed JSON input), and the skipped logic may result in empty fields or broken functionality. (80% fail)
