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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:43:46+00:00 2026-06-07T06:43:46+00:00

$json = ‘[ { class1: { function1:return \$a<b>$b</b>!\; } }, { class2: { function2:return

  • 0
$json = '[
    {
        "class1":
        {
            "function1":"return \"$a<b>$b</b>!\";"
        }
    },
    {
        "class2":
        {
            "function2":"return $b;",
            "function3":"return $c;"
        }
    }
]';
$decoded_classes = json_decode($json,true);
foreach($decoded_classes as $class)
{
    $class_name = key($class);
    if (is_array($class[$class_name]))
    {
        foreach($class[$class_name] as $function)
        {
            $function_name = key($class[$class_name]);
            $stack[$class_name][$function_name] = create_function($arr,$function);
        }
    } 
    else 
    {
        foreach($class as $function)
        {
            $c_name = key($class);
            $stack[$c_name] = create_function($arr,$function);
        }
    }
}
return $stack;

I need that this code will execute the same result but with only one foreach. How can i do that?

  • 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-06-07T06:43:49+00:00Added an answer on June 7, 2026 at 6:43 am

    Translating the JSON to… let’s say “PHPON”, you get:

    $decoded_classes = Array(
        Array(
            "class1"=>Array(
                "function1"=>'return "$a<b>$b</b>!";'
            )
        )
        Array(
            "class2"=>Array(
                "function2"=>'return $b;',
                "function3"=>'return $c;'
            )
        )
    );
    

    As you can see, your structure isn’t exactly the most efficient. You basically have one too many levels.

    Restructure the JSON like this:

    {     
        "class1": {     
            "function1":"return \"$a<b>$b</b>!\";"
        },
        "class2": {     
            "function2":"return $b;",
            "function3":"return $c;"
        }
    }
    

    In this way, the “PHPON” is:

    $decoded_classes = Array(
        "class1"=>Array(
            "function1"=>'return "$a<b>$b</b>!";'
        )
        "class2"=>Array(
            "function2"=>'return $b;',
            "function3"=>'return $c;'
        )
    );
    

    Now you don’t even need to loop at all. Accessing $decoded_classes['class1']['function1'] will give you that code. The only thing you have to do is convert those strings into callable functions, like so:

    array_walk_recursive($decoded_classes,function(&$f) {$f = create_function($arr,$f);});
    

    I can’t help but notice you didn’t define $arr anywhere in your code. You’ll have to specify $arr correctly for this to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The json_decode function expects strictly formed JSON and won't accept values without a key
JSON Parser Class public class JSONParser { static InputStream is = null; static JSONObject
Json.Net has no problem serializing an overridden property in a child class. public override
In JSON, when the client sends a numeric data type(with decimlas), what deserialization class
JSON Code: {date:2011.12.10, szombat ,route:Jászfényszaru - MISKOLC*,timetable:[{class_name: 2. ,cost1st:,destinationtime:07:50,reservation:-,distance:126 km,change:2,destination:Miskolc-Tiszai,details:[{starttime_real:05:37,starttime:05:37,platform:,traininfo:5729 személy (Szolnok - Hatvan),start:Jászfényszaru},{starttime_real:05:47,starttime:05:47,platform:1,traininfo:,start:Hatvan},{starttime_real:05:56,starttime:05:54,platform:4,traininfo:5500
JSON { mypage:{ outerwrapper:{ page:1, total:3, records:15, innerwrapper:{ rows:[ { id:1, read: true, cells:
JSON response is as follows: {approvals: [ {approval: { id:0121920, key:T100, value:Ben Tsu }
function json (url){ $.getJSON(url, function(data) { return data; }) } this function don't see
json_decode function is not part of PHP 5.1, so I cannot use it. Is
JSON stands for JavaScript Object Notation. But how come languages like php, java, c

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.