PHP json_encode token_get_all

发布于 2025-01-05 10:59:14 字数 1697 浏览 2 评论 0原文

我想以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-01-12 10:59:14

我想你真正想做的是生成一个字典列表。为此,您应该更喜欢普通的数组附加而不是 array_push:

foreach(token_get_all($code) as $c) {

    $tokens[] =
        array(
            "tokenName" => token_name($c[0]),
            "tokenValue" => $c[1],
            "tokenLine" => $c[2]
        );

}

节省一些临时变量并且更易于阅读。它会给你一个结果,例如:

[    
   {"tokenName":"T_COMMENT","tokenValue":"# some comment","tokenLine":"1"},
   {"tokenName":"T_VARIABLE","tokenValue":"$some_variable","tokenLine":"2"}
]

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:

foreach(token_get_all($code) as $c) {

    $tokens[] =
        array(
            "tokenName" => token_name($c[0]),
            "tokenValue" => $c[1],
            "tokenLine" => $c[2]
        );

}

Saves you a few temporary variables and is easier to read. It would give you a result such as:

[    
   {"tokenName":"T_COMMENT","tokenValue":"# some comment","tokenLine":"1"},
   {"tokenName":"T_VARIABLE","tokenValue":"$some_variable","tokenLine":"2"}
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文