Adding to array from different notation (tree.subtree)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Adding to array from different notation (tree.subtree)

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post by s.dot »

You would just add an element onto the array

Code: Select all

$spoke[] = $something
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

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

Post 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
Mickel-M
Forum Newbie
Posts: 1
Joined: Mon Jul 06, 2009 5:24 pm

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

Post 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;
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

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

Post by Darkzaelus »

I'm sorry, I asked a silly question. I'll go work it out tomorrow.

Cheers,

Darkzaelus
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

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

Post 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
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

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

Post 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
Post Reply