How do I use json variable in php.
$filePath = '/home/user/public_html/uploads/samplefile.txt'
echo '{"status":"success", "fileName" : "'.$filePath.'"}';
Say I would like to use it this way.
$mail->addattachment($fileName);
Thanks.
您需要首先将 json 数据反序列化为 PHP 数组。
$json_string = "...."; //this is your json string
$json_data = json_decode($json_string); // decode into an array
$mail->addattachment($json_data['filename']);
虽然@xbonez 的答案是正确的,但您可以按照您指定的方式使用它,但不建议这样做:
$json = <<<JSON
{"status":"success", "fileName" : "/home/user/public_html/uploads/samplefile.txt"}
JSON;
extract(json_decode($json, TRUE));
echo $fileName;