-2

Why am I getting these errors?

Warning: Illegal string offset 'en' in C:\xampp\htdocs\includes\stream.class.php on line 48

Warning: Illegal string offset 'en' in C:\xampp\htdocs\includes\stream.class.php on line 60

Here's my code.

$s['target_data']['title'] = $s['target_data']['title']['en'];
$s['target_data']['description'] = $s['target_data']['description']['en'];
Community
  • 1
  • 1
Ashley Williams
  • 95
  • 1
  • 1
  • 6
  • this error occured when i accidentally tried to use a $string variable considering it as an array and tried to add new values to array as `$string[] = $value['index']` – code builders Jan 16 '21 at 17:05

2 Answers2

4

The problem is that $s['target_data']['title'] is a string, not an array as you seem to be expecting.

PHP allows you to use array-type syntax to index into a string ($string[0] returns the first character of $string, for example), but that only works with numeric indexes like [0] - you cannot use string indexes like ["en"], which is what the error is complaining about.

The code you showed seems to be trying to convert a variable from an array to a string and storing it back in the same variable. Could you perhaps be running it twice - and then getting the error the second time because it's no longer an array?

jcsanyi
  • 8,133
  • 2
  • 29
  • 52
-1
   (array) $s['target_data']['title'] = (array) $s['target_data']['title']['en'];


   (array) $s['target_data']['description'] = (array)$s['target_data']['description']['en'];

if your $s['target_data']['description'] and $s['target_data']['title'] are not arrays, you can cast it as (array)

Ahmet Mehmet
  • 288
  • 2
  • 10
  • That might fix the error, but it'll always return `null` (and possibly generate a warning for the undefined array index) - not very useful in actually finding the `en` element. – jcsanyi Jun 29 '13 at 20:30
  • You're right, notice will be shown. If [en] index is null, so result always will be null. Then must be worked on [en] index – Ahmet Mehmet Jun 29 '13 at 20:39