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 6921355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:15:59+00:00 2026-05-27T10:15:59+00:00

When using PHP’s json_encode to encode an array as a JSON string, is there

  • 0

When using PHP’s json_encode to encode an array as a JSON string, is there any way at all to prevent the function from quoting specific values in the returned string? The reason I ask is because I need javascript to interpret certain values in the object as actual variable names, for example the name of an existing javascript function.

My end goal is to use the outputted json as the configuration object for an ExtJS Menu component, so the fact that everything gets quoted prevents me from successfully setting such properties as “handler” (click event handler function) of the child items arrays.

  • 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-27T10:15:59+00:00Added an answer on May 27, 2026 at 10:15 am

    This is what I ended up doing, which is pretty close to what Stefan suggested above I think:

    class JSObject
    {
        var $jsexp = 'JSEXP:';
    
        /**
         * Encode object
         * 
         * 
         * @param     array   $properties
         * @return    string 
         */
        function encode($properties=array())
        {
            $output    = '';
            $enc_left  = $this->is_assoc($properties) ? '{' : '[';
            $enc_right = ($enc_left == '{') ? '}' : ']';
    
            foreach($properties as $prop => $value)
            {
                //map 'true' and 'false' string values to their boolean equivalent
                if($value === 'true')  { $value = true; }
                if($value === 'false') { $value = false; }
    
                if((is_array($value) && !empty($value)) || (is_string($value) && strlen(trim(str_replace($this->jsexp, '', $value))) > 0) || is_int($value) || is_float($value) || is_bool($value))
                {
                    $output .= (is_string($prop)) ? $prop.': ' : '';
    
                    if(is_array($value))
                    {
                        $output .= $this->encode($value);
                    }
                    else if(is_string($value))
                    {
                        $output .= (substr($value, 0, strlen($this->jsexp)) == $this->jsexp) ?  substr($value, strlen($this->jsexp))  : '\''.$value.'\'';
                    }
                    else if(is_bool($value))
                    {
                        $output .= ($value ? 'true' : 'false');
                    }
                    else
                    {
                        $output .= $value;
                    }
    
                    $output .= ',';
                }
            }
    
            $output = rtrim($output, ',');
            return $enc_left.$output.$enc_right;
        }
    
        /**
         * JS expression
         * 
         * Prefixes a string with the JS expression flag
         * Strings with this flag will not be quoted by encode() so they are evaluated as expressions
         * 
         * @param   string  $str
         * @return  string
         */
        function js($str)
        {
            return $this->jsexp.$str;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP, is there a nice way to get the (parsed) introduction only from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Using PHP, is there a function/method/way to check if a variable contains something that
Using PHP, what's the fastest way to convert a string like this: 123 to
Using php-sdk I obtain pages in this way: $array = $this->instance->api( '/' . $fb_user
Using PHP, How to identify that the string is a path to any file?
Using PHP 5.3 if I send a JSON encoded string as an arugment via
Using php, is there a way to redirect to an existing html page with
Using PHP, is there a way to test if the browser is accessing a
Using PHP I'm attempting to take an HTML string passed from a WYSIWYG editor

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.