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

  • Home
  • SEARCH
  • 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 6039861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:25:52+00:00 2026-05-23T06:25:52+00:00

Here is an exerpt from the php_decoded JSON structure that I am working with

  • 0

Here is an exerpt from the php_decoded JSON structure that I am working with :

array(3) {
  ["$type"]=> string(51) "NanoWebInterpreter.WebInputData, NanoWebInterpreter"
  ["NBBList"]=>
    array(2) {
    ["$type"]=> string(81) "System.Collections.Generic.List`1[[monoTNP.Common.NBB, monoTNP.Common]], mscorlib"
    ["$values"]=>
    array(1) {
      [0]=>
      array(6) {
        ["$type"]=> string(34) "monoTNP.Common.NBB, monoTNP.Common"
        ["ID"]=> string(16) "id-0065-00000003"
        ["MPList"]=>
        array(2) {
          ["$type"]=> string(80) "System.Collections.Generic.List`1[[monoTNP.Common.MP, monoTNP.Common]], mscorlib"
          ["$values"]=>
           array(3) {
            [0]=>
            array(9) {
              ["$type"]=> string(43) "monoTNP.Common.EllipticalMP, monoTNP.Common"
              ["Eccentricity"]=> float(1)
              ["ID"]=> string(16) "id-0065-00000006"
              ["ParticleIndex"]=> int(-1)
              ["DispersionInteractionStrength"]=> float(0)
              ["DispersionInteractionRange"]=> float(2.5)
              ["CharacteristicSize"]=> float(0)
              ["CenterOfMass"]=> string(7) "<0,0,0>"
              ["OrientationVector"]=> string(2) "<>"
            }

I have been trying to write this function that recursively traces the JSON object and replaces the target value with $postvalue, but whenever I try to do this recursively, the value isn’t changed. Here is my code so far:

function replaceVal(&$json, $postkey, $postvalue, &$idCounter, $level)
{
        $depth = 3;

    #Base Case 
    #At the right depth level, check to see if the idCounter is equal to the
    #postkey value (the HTML input field name). If it is, take the  
    #corresponding key and assign the postvalue to it (the input from form).
    #Then return. Otherwise, incrememnt the idCounter and return.
        if ($level >= $depth){
                foreach($json as $key => $value){
                        if($idCounter == $postkey){
                                print "$key => $value\n";
                                $json[$key] = $postvalue; #Not working properly
                                return;
                        }
                        $idCounter++;
                }
        }

    #Descend down into the level of the tree specified by $depth. 
    #This level should correspond do the level at which the HTML input 
    #fields lie
    #$idCounter will only be greater than $postkey if the value has 
    #been changed by the previous if statement. In that case, the job is done
    #and the function will terminate.

        if ($level < $depth){
                foreach($json as $key => $value){
                        if ($idCounter < $postkey)
                                replaceVal($value, $postkey, $postvalue, $idCounter, $level+1);
                        else
                                return;
                }
         }
}

The interesting part is that if I directly index into the structure like so:

$key = &$json['NBBList']['$values'][0]['MPList']['$values'][0]['Eccentricity']
$key = "asdf";

The value can be changed. The only thing that seems to be the problem is the recursion. This sounds like a really easy problem to fix, but I’ve only been programming for a little less than a year so I am probably just missing something obvious. >.>

Oh and the postvalue and postkey values come from an HTML form submission.

–edit–
The print statement is just in there for debugging. It can be ignored.

Edit 2:
Here is how the function is called:

foreach ($_POST as $postkey => $postvalue)
{
        if ($postvalue != ""){
                print "$postkey => $postvalue\n";
                $idCounter = 1;
                replaceVal($json['NBBList']['$values'][0], $postkey, $postvalue, $idCounter, 0);
        }
}

Again, the print statement is for debugging purposes.
Additional info: The names of the HTML input fields are dynamically assigned numbers based on their order in the JSON tree. So, incrementing the variable idCounter corresponds to proceeding to the next input field.
Edit3: added in comments to code.

  • 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-23T06:25:53+00:00Added an answer on May 23, 2026 at 6:25 am

    You can (and should) allways use PHP’s internal function, in case there is any.

    If you don’t need the counter, you could look at array_replace_recursive. In that case, your code would look like this:

    function replaceVal(&$array, $key, $value) {
        $array = array_replace_recursive( $array, array( $key => $value ) );
    }
    

    EDIT

    After current comments:

    function replaceVal(&$json, $postkey, $postvalue, &$idCounter, $level)
    {
        $depth = 3;
    
        if ($level >= $depth){
                foreach($json as $key => $value) {
                        if($idCounter == $postkey) {
                                $json[$key] = $postvalue; #Not working properly
                                return;
                        }
                        $idCounter++;
                }
        }
    
        if ($level < $depth){
                foreach($json as $key => $value){
                        if ($idCounter < $postkey)
                                replaceVal($json[$key], $postkey, $postvalue, $idCounter, $level+1);
                        else
                                return;
                }
         }
    }
    

    The problem was that, in the recursion, you where using $value, wich is a copy of the array element. Then, that was edited, but the changes didn’t propagate to $json.

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

Sidebar

Related Questions

What did I do wrong? Here is an excerpt from my code: public void
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here's an excerpt from a fairly standard Rails form: <p> <%= f.label :from_station %>
Here's an excerpt from a book I'm reading about application design with MVC: Ideally,
Here's an excerpt from java.text.CharacterIterator documentation: This interface defines a protocol for bidirectional iteration
I'm getting an error here that says I haven't defined a method, but it

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.