Search code examples
phpjsonencoder

Getting value of php that is json_encode


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


Solution

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

    $allo = '[{"description":"BMW M3"}]';
    $test = json_decode($allo);
    echo $test[0]->description;