Ricardinho
Ricardinho

Reputation: 609

Getting [Trying to get property of non-object] when using ->

I want to get fruits:

'target' => '{"fruits":{"serves":[{"value":["apple"],"option":"first"}],"excludes":[{"value":["タグ2"],"option":"first"}]}}',

My code:

$target = $request->target;
$fruits = $target->fruits; //getting "Trying to get property of non-object" here

I tried json_decode($target->fruits) and $target['fruits'], but I got error again. What I do to get that fruits??

Upvotes: 0

Views: 55

Answers (2)

Dilip Hirapara
Dilip Hirapara

Reputation: 15316

you're getting value as in Multidimensional array so it'll be in array.

$targetarray = json_decode($request->target);

print_r($targetarray['fruits']);
    exit;

OR

print_r($target[0]['fruits']);
exit;

Upvotes: 1

Sandeep Sudhakaran
Sandeep Sudhakaran

Reputation: 1092

The problem is

$target = $request->target 

will return a string. you have to convert it into json obj.

for that you have to use json_decode();

$target = $request->target;
$targetJson = json_decode($target);
$fruits = $targetJson['fruits'];

try this, hope this will help you.

Upvotes: 3

Related Questions