0

Hello im trying to get the value of an element that is json_encoded.

  public function getDescription($noArticle){
  $stmt = $this->prepare("SELECT description FROM Inventaire WHERE noArticle = '{$noArticle}' ");
  $stmt->execute();
  $result = $stmt->fetchAll();
  return json_encode($result);

This returns me - > [{"description":"BMW M3"}] that is json_encoded.

I want to get only the "BMW M3"" part

I tried :

$allo = $allo->getDescription(1);
$test = json_decode($allo);
echo $test->{"description"};

not working if anyone could help me. Thanks

2
  • 2
    You have array of objects. Commented May 15, 2020 at 8:17
  • What about echo $test[0]->description;? Commented May 15, 2020 at 8:18

3 Answers 3

1

Your json is an array of objects, you should use:

$allo = '[{"description":"BMW M3"}]';
$test = json_decode($allo);
echo $test[0]->description;
Sign up to request clarification or add additional context in comments.

Comments

0

[{"description":"BMW M3"}] is an object within an array. So this should work:

echo $test[0]->description;

1 Comment

@Anasde That's nice to hear. Could you then follow protocol and upvote the correct answers? Since two people provided a correct answer you should also mark one of them as accepted :-)
0

Since your variable $allo is an array of one element, you should get this first element then get your object like this:

$test[0]->description

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.