PHP如何从变量中输出数组

t0ybt7op  于 2023-05-21  发布在  PHP
关注(0)|答案(2)|浏览(94)

我有一个名为“ulfana.txt”的文本文件,我想从中构建JSON字符串。
内容 Ulfana.txt

EAAAAAYsX7TsBAG5Ps8MA0ZCQZC3QLawZB29jINrChL5hPflXxalBiGW7ZAwpvUIs7qIzshLBRjBFvzAFGnhO3cXtZAZChW6XxtUITHXe9Q3aH6mfiGLoSsoqpGXqktpx2EBRcOcCqVCyufI7q0abl5RaZB2lE4MkVwDaThCTonm07WZB1dxTu4xbwuDdwn6ZArPUjroQWAcwJWBZA0PGmZCUitB

PHP代码:

<?php
    $tokens = file_get_contents('ulfana.txt'); //getting file content
    $fbid = '100011432913918'; //from $_SESSION['uid']
    echo '{ "token": "' . $tokens. '", "value": { "id": ' . $fbid . ', "name": "Ulfana.txt" } }';
?>

我想要这样的输出

{ "token":"EAAAAAYsX7TsBAG5Ps8MA0ZCQZC3QLawZB29jINrChL5hPflXxalBiGW7ZAwpvUIs7qIzshLBRjBFvzAFGnhO3cXtZAZChW6XxtUITHXe9Q3aH6mfiGLoSsoqpGXqktpx2EBRcOcCqVCyufI7q0abl5RaZB2lE4MkVwDaThCTonm07WZB1dxTu4xbwuDdwn6ZArPUjroQWAcwJWBZA0PGmZCUitB", "value": { "id": 100011432913918, "name": "Alif Mdza" } }
qyyhg6bp

qyyhg6bp1#

你可以这样做

$array=["token"=>$echo,"value"=>["id"=>$fbid,"name"=>'Ulfana.txt']];
$array=json_encode($array);
print_r($array);

输出为

uemypmqf

uemypmqf2#

<?php
    $tokens = file_get_contents('ulfana.txt');
    $fbid = '100011432913918';
    echo "{ \"token\": \"" . $tokens . "\", \"value\": { \"id\": " . $fbid . ", \"name\": \"Ulfana.txt\" } }";
?>

相关问题