PHP json_encode token_get_all
我想以 JSON 形式返回 PHP token_get_all() 函数。
我还希望 token_get_all 通过 token_name() 传递令牌函数来获取它的名字。
我尝试了各种不同的方法,但没有一个能产生我需要的结果。
我想在 JavaScript 中使用此信息,例如我希望能够调用 tokens.tokenName。
我想我需要类似以下示例的内容:
{
"tokenName":"T_COMMENT","tokenValue":"# some comment","tokenLine":"1"
"tokenName":"T_VARIABLE","tokenValue":"$some_variable","tokenLine":"2"
}
我尝试将 token_get_all() 函数直接通过 json_encode() 函数,以及使用各种数组,但结果不是我想要的。
这是代码的最新版本:
if (isset($_POST['code']) || (isset($_GET['code']))) {
if (isset($_POST['code'])) {
$code = $_POST['code'];
} elseif (isset($_GET['code'])) {
$code = $_GET['code'];
}
$tokens = array();
$tokenName = array();
$tokenValue = array();
$tokenLine = array();
foreach(token_get_all($code) as $c) {
if(is_array($c)) {
array_push($tokenName, token_name($c[0])); // token name
array_push($tokenValue, $c[1]); // token value
array_push($tokenLine, $c[2]); // token line number
} else {
array_push($tokenValue, $c); // single token, no value or line number
}
}
// put our token into the tokens array
array_push($tokens, $tokenName);
array_push($tokens, $tokenValue);
array_push($tokens, $tokenLine);
// return our tokens array JSON encoded
echo(json_encode($tokens));
}
谢谢你,
Ryan
I want to return the PHP token_get_all() function as JSON.
I also want token_get_all to pass the token through the token_name() function to get its name.
I have tried various different methods but none produce the results I need.
I'm wanting to use this information in JavaScript, I want to be able to call tokens.tokenName for example.
I think I need something like the following example:
{
"tokenName":"T_COMMENT","tokenValue":"# some comment","tokenLine":"1"
"tokenName":"T_VARIABLE","tokenValue":"$some_variable","tokenLine":"2"
}
I've tried to put the token_get_all() function directly through the json_encode() function, as well as playing around with various arrays and the results are not what I wanted.
This is the latest incarnation of the code:
if (isset($_POST['code']) || (isset($_GET['code']))) {
if (isset($_POST['code'])) {
$code = $_POST['code'];
} elseif (isset($_GET['code'])) {
$code = $_GET['code'];
}
$tokens = array();
$tokenName = array();
$tokenValue = array();
$tokenLine = array();
foreach(token_get_all($code) as $c) {
if(is_array($c)) {
array_push($tokenName, token_name($c[0])); // token name
array_push($tokenValue, $c[1]); // token value
array_push($tokenLine, $c[2]); // token line number
} else {
array_push($tokenValue, $c); // single token, no value or line number
}
}
// put our token into the tokens array
array_push($tokens, $tokenName);
array_push($tokens, $tokenValue);
array_push($tokens, $tokenLine);
// return our tokens array JSON encoded
echo(json_encode($tokens));
}
Thank you,
Ryan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你真正想做的是生成一个字典列表。为此,您应该更喜欢普通的数组附加而不是 array_push:
节省一些临时变量并且更易于阅读。它会给你一个结果,例如:
I guess what you actually want to do is generate a list of dictionaries. For that you should prefer ordinary array appending instead of
array_push
:Saves you a few temporary variables and is easier to read. It would give you a result such as: