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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:52:46+00:00 2026-06-03T14:52:46+00:00

I’m creating JSON encoded data from PHP arrays that can be two or three

  • 0

I’m creating JSON encoded data from PHP arrays that can be two or three levels deep, that look something like this:

[grandParent] => Array (
                       [parent] => Array (
                                         [child] => myValue 
                                      )
                    )

The method I have, which is simply to create the nested array manually in the code requires me to use my ‘setOption’ function (which handles the encoding later) by typing out some horrible nested arrays, however:

$option = setOption("grandParent",array("parent"=>array("child"=>"myValue")));

I wanted to be able to get the same result by using similar notation to javascript in this instance, because I’m going to be setting many options in many pages and the above just isn’t very readable, especially when the nested arrays contain multiple keys – whereas being able to do this would make much more sense:

$option = setOption("grandParent.parent.child","myValue");

Can anyone suggest a way to be able to create the multidimensional array by splitting the string on the ‘.’ so that I can json_encode() it into a nested object?

(the setOption function purpose is to collect all of the options together into one large, nested PHP array before encoding them all in one go later, so that’s where the solution would go)

EDIT: I realise I could do this in the code:

$options['grandparent']['parent']['child'] = "myValue1";
$options['grandparent']['parent']['child2'] = "myValue2";
$options['grandparent']['parent']['child3'] = "myValue3";

Which may be simpler; but a suggestion would still rock (as i’m using it as part of a wider object, so its $obj->setOption(key,value);

  • 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-03T14:52:47+00:00Added an answer on June 3, 2026 at 2:52 pm

    This ought to populate the sub-arrays for you if they haven’t already been created and set keys accordingly (codepad here):

    function set_opt(&$array_ptr, $key, $value) {
    
      $keys = explode('.', $key);
    
      // extract the last key
      $last_key = array_pop($keys);
    
      // walk/build the array to the specified key
      while ($arr_key = array_shift($keys)) {
        if (!array_key_exists($arr_key, $array_ptr)) {
          $array_ptr[$arr_key] = array();
        }
        $array_ptr = &$array_ptr[$arr_key];
      }
    
      // set the final key
      $array_ptr[$last_key] = $value;
    }
    

    Call it like so:

    $opt_array = array();
    $key = 'grandParent.parent.child';
    
    set_opt($opt_array, $key, 'foobar');
    
    print_r($opt_array);
    

    In keeping with your edits, you’ll probably want to adapt this to use an array within your class…but hopefully this provides a place to start!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.