-1

I get a strange (at least to me) error on my home page:

Warning: Illegal string offset 'en'

Here is the code, please help:

if (isset($s['target_data']['description']) && $s['target_data']['description']){
    if (isset($s['target_data']['description'][$lang])){
        $s['target_data']['description'] = $s['target_data']['description'][$lang];
    } else {
        $s['target_data']['description'] = $s['target_data']['description']['en'];
    }
} else {
    $s['target_data']['description'] = "";
}
Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
  • possible duplicate of [Illegal string offset Warning PHP](http://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – Jon Surrell Feb 19 '15 at 14:53

2 Answers2

0

The warning means that there is no:

$s['target_data']['description']['en']

set in your array.

You could fix it adding simple check:

<?php
if (isset($s['target_data']['description'])
    && $s['target_data']['description']
) {

    if (isset($s['target_data']['description'][$lang])) {
        $s['target_data']['description']
            = $s['target_data']['description'][$lang];
    } elseif (isset($s['target_data']['description']['en'])) { // here one more check added
        $s['target_data']['description']
            = $s['target_data']['description']['en'];
    }
    else { // nothing set
        $s['target_data']['description'] = '';
    }
} else {
    $s['target_data']['description'] = "";
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

What does that error say?

Warning: Illegal string offset 'en'

You're trying to access a string with the offest en. Just like you would access an array. Except strings aren't going to support named keys...

I assume you're expecting $s['target_data']['description'] to be an array. Try checking for that, and that the key exists:

// THIS:
//
// } else {
//   $s['target_data']['description'] = $s['target_data']['description']['en'];
// }
//
// BECOMES:
//
elseif (is_array($s['target_data']['description']) && $s['target_data']['description']['en']) {
    $s['target_data']['description'] = $s['target_data']['description']['en'];
}
Jon Surrell
  • 9,444
  • 8
  • 48
  • 54