articles as $articleObject) { if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) { $returnVal = $articleObject->name . "\n". $articleObject->price . "€"; $articleObject->quantity = strval(intval($articleObject->quantity) - 1); } } // open the file and write the updated stock $fp = fopen('../data/stock.json', 'w'); fwrite($fp, json_encode($result)); fclose($fp); echo $returnVal;