Page 1 of 1

How to map a file to multidimensional array?

Posted: Wed Jun 02, 2004 2:10 pm
by basdog22
Hi .
Assume i have this code in a file called array.txt

Code: Select all

<?php
array(
    1 => array(
        'title' => 'Menu item 1', 
        'url' => '/item1.php',
        'sub' => array(
            11 => array('title' => 'Menu item 1.1', 'url' => '/item1.1.php'),
            12 => array(
                'title' => 'Menu item 1.2', 
                'url' => '/item1.2.php',
                'sub' => array(
                    121 => array('title' => 'Menu item 1.2.1', 'url' => '/item1.2.1.php'),
                    122 => array('title' => 'Menu item 1.2.2', 'url' => '/item1.2.2.php')
                )
            )
        )
    ),
    2 => array(
        'title' => 'Menu item 2', 
        'url' => '/item2.php',
        'sub' => array(
            21 => array('title' => 'Menu item 2.1', 'url' => '/item2.1.php'),
            22 => array('title' => 'Menu item 2.2', 'url' => '/item2.2.php')
        )
    )
);
?>
and i want to map it on a real array like $array. I mean the $array will be:

Code: Select all

$array=array(1 => array(
        'title' => 'Menu item 1', 
        'url' => '/item1.php',
        'sub' => array(
            11 => array('title' => 'Menu item 1.1', 'url' => '/item1.1.php'),
            12 => array(
                'title' => 'Menu item 1.2', 
                'url' => '/item1.2.php',
                'sub' => array(
                    121 => array('title' => 'Menu item 1.2.1', 'url' => '/item1.2.1.php'),
                    122 => array('title' => 'Menu item 1.2.2', 'url' => '/item1.2.2.php')
                )
            )
        )
    ),
    2 => array(
        'title' => 'Menu item 2', 
        'url' => '/item2.php',
        'sub' => array(
            21 => array('title' => 'Menu item 2.1', 'url' => '/item2.1.php'),
            22 => array('title' => 'Menu item 2.2', 'url' => '/item2.2.php')
        )
    )
);
How can this be done???
I tried eval() but it does not work as expected.

Posted: Wed Jun 02, 2004 2:14 pm
by jason
Why would you want to do something like this?

Posted: Wed Jun 02, 2004 2:17 pm
by basdog22
I want to create a menu which can be edited by the user. So if the array in the txt changes the menu structure changes too.

I use PEAR's html_menu class.

Posted: Wed Jun 02, 2004 2:28 pm
by patrikG
Maybe it's because I've stared at screens all day, but I don't see a real difference in your two code samples. Both are valid multi-dimensional arrays and can be accessed via their responding keys. I don't know what you actually want to do.

Posted: Wed Jun 02, 2004 2:30 pm
by markl999
Seems a lot of work when the first one just needs $array= on the front of it (that's the only difference i see)
*shrug*

Posted: Wed Jun 02, 2004 2:36 pm
by basdog22
No you didn't understand what i want to do.

I want the $array variable to hold the same values when getting them from the txt file as it would as a normal array:

when i open the txt file and read it with php i get the

Code: Select all

<?php
array(
    1 => array(
        'title' => 'Menu item 1',
        'url' => '/item1.php',
        'sub' => array(
            11 => array('title' => 'Menu item 1.1', 'url' => '/item1.1.php'),
            12 => array(
                'title' => 'Menu item 1.2',
                'url' => '/item1.2.php',
                'sub' => array(
                    121 => array('title' => 'Menu item 1.2.1', 'url' => '/item1.2.1.php'),
                    122 => array('title' => 'Menu item 1.2.2', 'url' => '/item1.2.2.php')
                )
            )
        )
    ),
    2 => array(
        'title' => 'Menu item 2',
        'url' => '/item2.php',
        'sub' => array(
            21 => array('title' => 'Menu item 2.1', 'url' => '/item2.1.php'),
            22 => array('title' => 'Menu item 2.2', 'url' => '/item2.2.php')
        )
    )
);
?>
code. I want the code parsed so that the $array var has the generated array.

Damn too confusing. :roll: :roll:

Posted: Wed Jun 02, 2004 2:40 pm
by patrikG
[php_man]file[/php_man], [php_man]fopen[/php_man] or [php_man]readfile[/php_man] would help you there.

Posted: Wed Jun 02, 2004 2:54 pm
by basdog22
file, fopen or readfile would help you there.
This is how i read the txt file and gives me the code. I did this. The problem is that when i print_r($array) i get the same code and not the array elements.

BTW: My session expires when i live the forum(not closing the browser). It started since the layout change. Not even "remember my login" works.

Posted: Wed Jun 02, 2004 2:55 pm
by markl999
I'd just change array.txt so it says $array = array( etc.. then do
require_once 'array.txt';

Then $array becomes available as a normal var, eg echo $array[1]['title'];

Posted: Wed Jun 02, 2004 3:00 pm
by basdog22
I'd just change array.txt so it says $array = array( etc.. then do
require_once 'array.txt';
It works. thanks to all. It was so simple and i was trying to do it the hard way. :oops:

Thanks :D