Page 1 of 1

Adding to array from different notation (tree.subtree)

Posted: Mon Jul 06, 2009 4:20 pm
by Darkzaelus
If i have a string like:

Code: Select all

$path="Verbs.State";
and have an array called Examples:

Code: Select all

$examples=array();
How do I add "State" into the array like so:

Code: Select all

$examples=array("State"=>array());
without having to resort to array_merge_recursive, as the examples variable is actually part of a larger array?

Basically i'm trying to create an array of examples, for which they can be added into groups of examples. After this I would add examples into the group, like such (including $example's place in the full array):

Code: Select all

$spoke=array("label"=>"test", "index"=>1, "examples"=>array("State"=>array("example 1")));
I appreciate any time you lend me :)

Darkzaelus

Re: Adding to array from different notation (tree.subtree)

Posted: Mon Jul 06, 2009 4:25 pm
by s.dot
You would just add an element onto the array

Code: Select all

$spoke[] = $something

Re: Adding to array from different notation (tree.subtree)

Posted: Mon Jul 06, 2009 4:33 pm
by Darkzaelus
But does that not just add a value? What i'm looking for is adding a key and a value.

Cheers,

Darkzaelus

EDIT: here is the function code so far:

Code: Select all

public function addExample($name, $example) {
    $parts=explode('.', $name);
    $path=$this->spokes[$parts[0]]['examples'];
    for($i=1,$b=sizeof($parts);$i<$b;$i++)
        $path=$path[$parts[$i]];
    $path=$example;
}
If I were to put "Verbs.State" into $name, and "test" into $example, I would expect the following the happen:
  • Check to see if the tree "State" appears in the spoke object ($this->spokes[$parts[0]]['examples'] (array with "examples" array in it))
  • If it does not exist, create the path.
  • Add in the example in the tree (e.g. spoke "Verbs", tree in examples array of value "State", which is also an array, and "test" as a static value inside that array).
Cheers,

Darkzaelus

Re: Adding to array from different notation (tree.subtree)

Posted: Mon Jul 06, 2009 5:39 pm
by Mickel-M
If you want to add both the key and value, I think it's something like:

Code: Select all

$var['key'] = $value;

Re: Adding to array from different notation (tree.subtree)

Posted: Mon Jul 06, 2009 5:44 pm
by Darkzaelus
I'm sorry, I asked a silly question. I'll go work it out tomorrow.

Cheers,

Darkzaelus

Re: Adding to array from different notation (tree.subtree)

Posted: Tue Jul 07, 2009 2:35 am
by Darkzaelus
The point with this is that because i'm dealing with a multi-level structure, if I just add them in using array_push like described or just add in a new entry, then it would overwrite any examples already written. I've tried constructing the path which would end up something like:

Code: Select all

$this->spokes['Verbs']['examples']['State'][...]
//done with:
$parts=explode('.', 'Verbs.State');
$path=$this->spokes['Verbs']['examples'];
for($i=1,$b=sizeof($parts);$i<$b;$i++)
    $path=$path[$parts[$i]];
$path='test';
but then changes made to that do not reflect back in the $this->spokes['Verbs']['examples'] array.

I then tried to use array_merge_recursive to stick the path back into the original array, but then you get duplicates if there is more than one example tree.

Is there any way apart from setting it up with:

Code: Select all

eval('$this->spokes["Verbs"]["examples"]["State"]=array();');
and then adding in subsequent subtrees in with the same format, where I would build the array path into a string, and then call it into eval.

Cheers,

Darkzaelus

Re: Adding to array from different notation (tree.subtree)

Posted: Tue Jul 07, 2009 4:34 pm
by Darkzaelus
Right! the code is finished with the eval() method:

Code: Select all

public function addExample($name, $example) {
    $parts=explode('.', $name);
    $path="\$this->spokes['{$parts[0]}']['examples']";
    for($i=1,$b=sizeof($parts);$i<$b;$i++) {
        $path.='['.$parts[$i].']';
        if(!eval('isset('.$path.');'))
            eval($path.'=array();');
    }
    eval($path.'[]="'.$example.'";');
    $path=$example;
}
Cheers for all the help,

Darkzaelus