title: Converting json to array with recursive method?
body: <p>I am trying to convert json string inside an array into array,</p>
  
  <pre><code>$config = array(
      "type"  =&gt; '{"category":"admin","page":"page"}',
      "say"     =&gt; "Hello",
      "php"   =&gt; array(
          "say"     =&gt; "no",
          "type"  =&gt; '{"category":"admin","page":"page"}',
          "gran"  =&gt;array(
              "name" =&gt; "Hi"
          )
      )
  );
  </code></pre>
  
  <p>My working code,</p>
  
  <pre><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 =&gt; $value)
          {
              if($recursive === false) $array[$key] = (!empty($value) &amp;&amp; is_string($value) &amp;&amp; json_decode($value) != NULL) ? json_decode($value, true): $value;
                  else $array[$key] = (!empty($value) &amp;&amp; is_string($value) &amp;&amp; json_decode($value) != NULL) ? json_decode($value, true): is_array($value) ? self::json_to_array($array) : $value;
          }
  
          return $array;
      }
  }
  </code></pre>
  
  <p>It works fine <em>without</em> <strong>recursive method</strong> but breaks when I want to do the <strong>recursive</strong> as you can see in my code above,</p>
  
  <pre><code>$object = new objectify();
  $config = $object-&gt;json_to_array($config);
  print_r($config);
  </code></pre>
  
  <p>error message,</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I just want to get this result,</p>
  
  <pre><code>Array
  (
      [type] =&gt; Array
          (
              [category] =&gt; admin
              [page] =&gt; page
          )
      [say] =&gt; Hello
          (
              [say] =&gt; no
              [type] =&gt; {"category":"admin","page":"page"}
              [gran] =&gt; Array
                  (
                      [name] =&gt; Hi
                  )
  
          )
  
  )
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>$config = 'type={"category":"admin","page":"page"}&amp;text_editor={"name":"mce-basic"}&amp;parent_id=self&amp;subtitle=true&amp;description=true&amp;content_1=true&amp;script_1=true&amp;primary_image=true';
  parse_str($config,$array);
  print_r($array);
  </code></pre>
  
  <p>result,</p>
  
  <pre><code>Array
  (
      [type] =&gt; {"category":"admin","page":"page"}
      [text_editor] =&gt; {"name":"mce-basic"}
      [parent_id] =&gt; self
      [subtitle] =&gt; true
      [description] =&gt; true
      [content_1] =&gt; true
      [script_1] =&gt; true
      [primary_image] =&gt; true
  )
  </code></pre>