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

Hans design5 at softflow.co.uk
Sat Feb 7 11:48:12 CST 2009


Saturday, February 7, 2009, 4:46:35 PM, Daniel wrote:

> Just add an else statement that creates the that key, then point to
> that key for the next iteration.

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

Thanks! It nearly worked. But the last element was always added as

[last] => array ( 'last' => 'value')

instead of just 'last' => 'value'
because the else condition gets hit with every last element.
So I added a condition so the last iteration will not process
the 'else'. It looks like that in my code:

        foreach ($keys as $i => $k) {
                if (isset($arr[$k]))
                                $arr = &$arr[$k];
                else {
                        if ($i < count($keys)-1) {
                                $arr[$k] = '';
                                $arr = &$arr[$k];
                        }
                }
        }


> I should probably ask if this is for a PmWiki recipe or something
> having to do with PmWiki. It's become quite a long thread, and this
> mailing list is for PmWiki development discussions. So if it's just a
> general php question, it might be better to continue our discussion
> elsewhere.

I am working on a PmWiki recipe for processing multi-dimensional
arrays or data trees.


Hans




More information about the pmwiki-devel mailing list