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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:35:29+00:00 2026-06-03T04:35:29+00:00

I am trying to convert json string inside an array into array, $config =

  • 0

I am trying to convert json string inside an array into array,

$config = array(
    "type"  => '{"category":"admin","page":"page"}',
    "say"     => "Hello",
    "php"   => array(
        "say"     => "no",
        "type"  => '{"category":"admin","page":"page"}',
        "gran"  =>array(
            "name" => "Hi"
        )
    )
);

My working code,

class objectify
{

    public function json_to_array($array, $recursive = true)
    {
        # if $array is not an array, let's make it array with one value of former $array.
        if (!is_array($array)) $array = array($array);

        foreach($array as $key => $value)
        {
            if($recursive === false) $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL) ? json_decode($value, true): $value;
                else $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL) ? json_decode($value, true): is_array($value) ? self::json_to_array($array) : $value;
        }

        return $array;
    }
}

It works fine without recursive method but breaks when I want to do the recursive as you can see in my code above,

$object = new objectify();
$config = $object->json_to_array($config);
print_r($config);

error message,

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2048 bytes) in C:\wamp\www\test\2012\php\set_variable.php on line 79

I just want to get this result,

Array
(
    [type] => Array
        (
            [category] => admin
            [page] => page
        )
    [say] => Hello
        (
            [say] => no
            [type] => {"category":"admin","page":"page"}
            [gran] => Array
                (
                    [name] => Hi
                )

        )

)

EDIT:

$config = 'type={"category":"admin","page":"page"}&text_editor={"name":"mce-basic"}&parent_id=self&subtitle=true&description=true&content_1=true&script_1=true&primary_image=true';
parse_str($config,$array);
print_r($array);

result,

Array
(
    [type] => {"category":"admin","page":"page"}
    [text_editor] => {"name":"mce-basic"}
    [parent_id] => self
    [subtitle] => true
    [description] => true
    [content_1] => true
    [script_1] => true
    [primary_image] => true
)
  • 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-03T04:35:30+00:00Added an answer on June 3, 2026 at 4:35 am

    Quick solution:

    $full_array = array_map('json_decode', $array);
    

    If not all your parameters are JSON, and you want actual arrays instead of stdClass objects, you may have to do this:

    function json_decode_array($input) { 
      $from_json =  json_decode($input, true);  
      return $from_json ? $from_json : $input; 
    }
    $full_array = array_map('json_decode_array', $array);
    

    If you have more levels of nested arrays outside of the JSON, then you have to do your own recursion. Try this version of objectify:

    class objectify
    {
      public function json_mapper($value, $recursive = true) {
        if (!empty($value) && is_string($value) &&
            $decoded = json_decode($value, true)) {
          return $decoded;
        } elseif (is_array($value) && $recursive) {
          return array_map('objectify::json_mapper', $value);
        } else {
          return $value;
        }
      }
    
      // currying, anyone?
      public function json_mapper_norecurse($value) { 
         return objectify::json_mapper($value, false);
      }
    
      public function json_to_array($array, $recursive = true)
      {
        # if $array is not an array, let's make it array with one value of
        # former $array.
        if (!is_array($array)) {
          $array = array($array);
        }
    
        return array_map(
          $recursive ? 'objectify::json_mapper' 
                     : 'objectify::json_mapper_norecurse', $array);
      }
    }
    

    Which works fine for me on both of your sets of sample data.

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

Sidebar

Related Questions

I'm new in iPhone and I'm trying to convert NSMutable array to json string
Trying to convert a JSON string into an object in C#. Using a really
Hello i'm trying to convert my Java Object to a Json string to use
I am trying to convert a JSOn string into a Mongo document and there
I am querying a database for results and trying to convert them into JSON
I am trying to convert a mySQL query into a JSON object. This works:
im trying to convert a json object to a string by using gson here
How to extract and save array from string parameter? I'm trying convert string beafore_create
I'm trying to parse JSON string using Boost Spirit store JSON object into recursive
I'm trying to convert a JSON object/array from the Google Directions API v3 to

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.