0

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.

4

2 回答 2

0

您需要首先将 json 数据反序列化为 PHP 数组。

$json_string = "...."; //this is your json string

$json_data = json_decode($json_string); // decode into an array
$mail->addattachment($json_data['filename']);
于 2013-09-06T01:42:33.240 回答
0

虽然@xbonez 的答案是正确的,但您可以按照您指定的方式使用它,但不建议这样做:

$json = <<<JSON
{"status":"success", "fileName" : "/home/user/public_html/uploads/samplefile.txt"}
JSON;

extract(json_decode($json, TRUE));

echo $fileName;
于 2013-09-06T01:52:22.923 回答