Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7505251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:50:55+00:00 2026-05-29T21:50:55+00:00

I want to return the PHP token_get_all() function as JSON. I also want token_get_all

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:50:56+00:00Added an answer on May 29, 2026 at 9:50 pm

    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"}
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that makes a random token (A-Z, a-z, 0-9): function
I want use php curl with oauth to get the JSON data from twitter
function getIp() { return <?php echo $_SERVER['REMOTE_ADDR']; ?>; } I want to adapt or
I want to return a private field from a remoted object but i get
I want to return a set of values from function till the point they
I want to return the Model (data) of a controller in different formats (JavaScript/XML/JSON/HTML)
I want to return ArrayList<HashMap<String,String>> in EL function with three String argument. How to
I am successfully parsing an RSS feed with PHP, but want to return a
I have a winForms NET3.5SP1 app, and want to POST data to a PHP
I want to return field name from a database table where id = 4.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.