Page 1 of 1

How to Update Values in Multidimensional Array?

Posted: Thu Jun 25, 2009 11:09 am
by gemini
I have a multidimensional array, which serves default values to a form, and the form is actually being built from this array. Now, I want to be able to change the values in the form and update the array on submit. My problem is, that the forms input fields are named by the end keys of the array, so I can not access them directly in the array since they can be located very deep in it. I can do a recursive search by the key (which is the name of the field in the form) and find the existing values of the array, but I don't know how to update them and return the new (updated) array.

I'd appreciate any help. Thank you!

Re: How to Update Values in Multidimensional Array?

Posted: Thu Jun 25, 2009 2:29 pm
by pickle
I'm not completely sure what you're asking. What does the array look like that you're trying to update.

If your question can be reworded as "How do I change a value in a multidimensional array", you do it this way:

Assume your array is like so:

Code: Select all

Array
(
    [0] => Array
        (
            [name] => Array
                (
                    [first] => Joe
                    [last] => Smith
                )
 
            [number] => (123)456.7890
        )
)
You would change the last name like this:

Code: Select all

$myArray[0]['name']['last'] = 'Anderson';

Re: How to Update Values in Multidimensional Array?

Posted: Thu Jun 25, 2009 3:37 pm
by gemini
Thanks for the reply pickle.
You understood the question correctly. My array looks very similar to what you shown but much more deeper and complicated.
From your example: $myArray[0]['name']['last'] = 'Anderson';
My form input field has a name "last", so when the form is submitted I get $_POST['last']. Now, in order to update the array from your example I need to know the parent keys [0]['name'] that I could form this like $myArray[0]['name']['last'] = $_POST['last']; My problem is - I don't know those keys - the array is too complicated and I have too many values to update, so I can't create lines like that manually. I can do a recursive search in the array, to find old values by key (in our example 'last'), but I don't know how to update the array in this case.

Not sure if it's clear enough or not. In a 2 or 3 level array I could have field names like this

Code: Select all

<input name="table[employee][personal_record][first_name]" type="text" />
this way I could apply $_POST[table][employee][personal_record][first_name] to my array, but in my case I only have $_POST['first_name'] - I'm missing all the parent arrays keys.

Re: How to Update Values in Multidimensional Array?

Posted: Mon Jun 29, 2009 10:04 am
by pickle
If you've only got one occurrence of $_POST['first_name'], why bother trying to denote all the parent keys? I've had projects in the past where the way the data was stored in the form was different than the way the data was stored in the PHP script. There were lots of problems trying to convert one format to the other. Eventually I just decided "screw it" and rewrote the PHP script to accept and deal with data in the format the form delivered it.

If I'm completely missing the point, could you please post a small snippet of your $_POST data and a small chunk of your form & explain how the two need to match up?

Re: How to Update Values in Multidimensional Array?

Posted: Mon Jun 29, 2009 5:17 pm
by gemini
My example was not totally accurate.
I don't want to publish specifics of the project, so let me explain it in a little different way:

I have a node with standard parameters (options that will be changed via a form).
$node = array(param1, param2, param3...);

Then there is a hierarchical structure, like a tree where those nodes saved with different values. As you see, the node array keys are the same, only the values are different. The hierarchy is very important as it's being used to store nodes in a certain parent/child order and then output in that specific way.

The form, is just a mechanism of changing those node values.

So, here is example of the array:

Code: Select all

 
$tree = array(
  node1 => array(
    param1 => a,
    param2 => b,
    param3 => c,
    child => array(
      node_1 => array (
        param1 => e,
        param2 => r,
        param3 => t,
        child => array(
          node_2 => array (
            param1 => 1,
            param2 => 2,
            param3 => 3,
            child => 
          ), 
        ),
    ),
  node2 => array(
    param1 => h,
    param2 => j,
    param3 => k,
    child => 
  );
);
 
The number and depth of nodes and their children is unlimited. As you can see some nodes can have no children at all. But the hierarchy is very important.

I'm totally new to classes and objects. I've used them before, but not in an extensive way. I have a class that turns the whole $tree into an object and every single node as well. Since the $_POST['node_name'] is the key of the actual node, I thought I might be able to have an "update" method in a class, that searches for the node key and updates its parameters to the $_POST['node_name']. I could not get the same thing to work in a regular recursive search function, but I think in a class, inside of a recursive update method by using $this->node it might actually work. I'm not sure.

Another important feature I need to implement - I need to be able to move nodes within the tree structure. Kind of like from sub folders into an upper level folder or a completely different folder within the same tree.

I hope this is more clear explanation of my dilemma :)

Re: How to Update Values in Multidimensional Array?

Posted: Mon Jun 29, 2009 5:33 pm
by pickle
Ok, so in your $_POST, you'll have $_POST['node_1'] that you want to map to a node that is somewhere in your tree, corresponding to the array element keyed with "node_1". I noticed in your example you have each node name being unique. I'm going to assume that's by design.

I don't think you need to put them all in objects - a recursive function should work fine. Note that this function still needs a lot of work & hasn't been tested, but hopefully points you in the right direction - assuming I'm now on the same page as you.

Code: Select all

$target_node = "node2";
$tree = array("lets assume the example array you posted");
 
findNode($tree,$target_node);
 
function findNode(&$tree,$target_node)
{
  foreach($tree as $name=>$node)
  {
    if($name == $target_node)
    {
      updateNode($tree,$target_node);
      return;
    }
  }
  findNode($node['child'],$target_node);
}
 
function updateNode(&$tree,$target_node)
{
  $tree[$target_node] = 'updated';
}
 

Re: How to Update Values in Multidimensional Array?

Posted: Mon Jun 29, 2009 5:42 pm
by gemini
Thanks a lot pickle! I'll try it out and let you know how it works out. And yes, you assumed correctly, the node keys are unique in the tree. Thanks again!

Re: How to Update Values in Multidimensional Array?

Posted: Thu Jul 02, 2009 7:11 pm
by gemini
Was sick for a week, couldn't get to this project. I looked at the function and it's what I was looking for. To get it to work I had to move line 16 of your example into the foreach loop. Thanks a lot for your help! :)

Re: How to Update Values in Multidimensional Array?

Posted: Thu Jul 02, 2009 7:26 pm
by gemini
Well, I'm still facing the same problem - how do I return the whole array with the updated nodes?

Re: How to Update Values in Multidimensional Array?

Posted: Thu Jul 02, 2009 7:40 pm
by gemini
Ok! Got it to work - false alarm :) Thanks again for your help!