anoja madusanka
anoja madusanka

Reputation: 149

Illegal String offset in php (Laravel 5.1)

In lang.php file has the following array.

"animal" => [
           "mammel"=>[
              "cat" => "Cat",
              "ele" => "Elephant",
              "dog" => "Dog"
            ] 
        ]




$r_animal = str_ireplace($lm, trans('lang.animal.mammel')[strtolower(substr($lm, 0, 3))], $r_animal);

In above line give the error as Illegal String offset 'ele' .

Upvotes: 0

Views: 346

Answers (1)

user8034901
user8034901

Reputation:

To access the translations you would need something like trans('lang.animal.mammel.cat'), trans('lang.animal.mammel.ele') or trans('lang.animal.mammel.dog'). Assuming strtolower(substr($lm, 0, 3)) contains either "cat", "ele" or "dog" you could concatenate the required translation key like so:

$translationKey = 'lang.animal.mammel.' . strtolower(substr($lm, 0, 3));
$r_animal = str_ireplace($lm, trans($translationKey), $r_animal);

Upvotes: 1

Related Questions