articles as $articleObject) { if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) { $price = $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 $price;