How to map a file to multidimensional array?

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
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

How to map a file to multidimensional array?

Post 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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Why would you want to do something like this?
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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*
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post 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:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]file[/php_man], [php_man]fopen[/php_man] or [php_man]readfile[/php_man] would help you there.
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post 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.
Last edited by basdog22 on Wed Jun 02, 2004 2:56 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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'];
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

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