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

Anasde
  • 17
  • 3

3 Answers3

1

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

$allo = '[{"description":"BMW M3"}]';
$test = json_decode($allo);
echo $test[0]->description;
Buddy Christ
  • 1,364
  • 8
  • 22
0

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

echo $test[0]->description;
Ben Hillier
  • 2,126
  • 1
  • 10
  • 15
  • @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 :-) – Ben Hillier May 15 '20 at 09:30
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
solikhay
  • 26
  • 3