[pmwiki-devel] php multi-dimensional array construction problem

Daniel Roesler diafygi at gmail.com
Fri Feb 6 10:29:06 CST 2009


Hold on there pal, we're not done yet. If you are running php5 you can
use references (I call them pointers).

//This is the original tree array
$tree = array("k1"=>array("k2"=>array("k3"=>"foo")));

//This creates a reference to that array
$pointer = &$tree;

//This parses the query string
$query_string = "k1:k2:k3";
$query = explode(":",$query_string);

//This changes the pointer to the leaf
foreach($query as $i) {
	if(isset($pointer[$i]))
		$pointer = &$pointer[$i];
}

//Now $pointer is the leaf, so you can
//do whatever you want to it.
$pointer = "bar"; //Changed the leaf of $tree to 'bar'

//As you can see the leaf of $tree has been changed.
print_r($tree);


Avast!
Daniel Roesler
diafygi at gmail.com

On Fri, Feb 6, 2009 at 9:32 AM, Hans <design5 at softflow.co.uk> wrote:
> Friday, February 6, 2009, 2:16:15 AM, Patrick wrote:
>
>> What happens with...?
>
>>     a:b:c = val1
>>     a:b   = val2
>
>> It would seem that array[a][b] can't simultaneously be a value and an array.
>
> True. I would probably want to check if a value (or array) exists
> before setting a new value, so it just gets added at the right level.
>
>
> I like to thank everyone for their ideas! None worked for me, but I
> got inspired by each response, and took something from it!
> I think I got a solution I can share here now:
>
> The functions make it possible to pass the keys either as array or as
> string with ':' separators between the words.
>
> For getting a node value the function cuts the array tree iteratively
> smaller till the leaf node is left:
>
> function TreeNodeGet($keys, $tree) {
>        if (!$keys) return null;
>        if (!is_array($keys))
>                $keys = explode(":", $keys);
>        while ($key = array_shift($keys)) {
>                if (!array_key_exists($key, $tree)) return null;
>                $tree = $tree[$key]; //reduce branch iteratively
>        }
>        return implode("",$tree); //node leaf
> } //}}}
>
>
> But I could not figure a way to use iterations for setting a new
> value, I kept messing up the original array. So here I am using eval,
> which should not pose problems with input restricted to safe
> characters. This function will just overwrite an existing value:
>
> function TreeNodeSet($keys, &$tree, $val) {
>        if (is_array($keys))
>                $keys = implode(":", $keys);
>        return eval( '$tree[\''.str_replace(':', '\'][\'', $keys).'\'] = $val; return true;');
> } //}}}
>
>
> Then to add some checks for preventing overwriting two more
> functions, also using eval to pass the keys:
>
> function TreeNodeExists($keys, $tree) {
>        if (is_array($keys)) $keys = implode(":", $keys);
>        return eval( 'if(isset($tree[\''.str_replace(':', '\'][\'', $keys).'\'])) return true; else return false;');
> } //}}}
>
> function TreeNodeIsArray($keys, $tree) {
>        if (is_array($keys)) $keys = implode(":", $keys);
>        return eval( 'if(is_array($tree[\''.str_replace(':', '\'][\'', $keys).'\'])) return true; else return false;');
> } //}}}
>
>
> There are probably better php solutions out there.
> But stuff I found about iteration classes I could not understand.
> Most info seems to be about accessing or writing hierarchical data
> structures to a SQL db.
>
> Hans
>
>
> _______________________________________________
> pmwiki-devel mailing list
> pmwiki-devel at pmichaud.com
> http://www.pmichaud.com/mailman/listinfo/pmwiki-devel
>



More information about the pmwiki-devel mailing list