dynamically generating a multidemensional array

This is a discussion on dynamically generating a multidemensional array within the PHP Language forums, part of the PHP Programming Forums category; I have an array which is dynamically generated by parsing a web page: titles Array ( [0] => bookmarks [1] => ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-12-2004
Red
 
Posts: n/a
Default dynamically generating a multidemensional array

I have an array which is dynamically generated by parsing a web page:
titles Array
(
[0] => bookmarks
[1] => New Folder
[2] => New Folder2
)
and I want to insert html links into another, nultidemensional array
which is based on the first array:
$dl[$titles[0]][$titles[1]][$titles[2]]=$link;

However this only works if the titles array has exactly 3 members.

So I made this switch:
$num=sizeof($titles);
switch($num){
case 1:
$dl[$titles[0]][]=$link;
break;
case 2:
$dl[$titles[0]][$titles[1]][]=$link;
break;
case 3:
$dl[$titles[0]][$titles[1]][$titles[2]][]=$link;
break;
}

This works pretty good as long as I expand the switch to have more cases
than largest possible size for the titles array. However, I can't help
feeling that there must be a better way, something which would custom
generate the multidemensional array each time I insert data into it.

This is rediculous, but it might give you the idea of what I am looking
for:
foreach($titles as $title){
$dl.="[$title]";
}

$dl[]=$link;

Is this possible in PHP ?

red
Reply With Quote
  #2 (permalink)  
Old 08-12-2004
C.J. Garibay
 
Posts: n/a
Default Re: dynamically generating a multidemensional array

Red wrote:

> I have an array which is dynamically generated by parsing a web page:
> titles Array
> (
> [0] => bookmarks
> [1] => New Folder
> [2] => New Folder2
> )
> and I want to insert html links into another, nultidemensional array
> which is based on the first array:
> $dl[$titles[0]][$titles[1]][$titles[2]]=$link;
>
> However this only works if the titles array has exactly 3 members.
>
> So I made this switch:
> $num=sizeof($titles);
> switch($num){
> case 1:
> $dl[$titles[0]][]=$link;
> break;
> case 2:
> $dl[$titles[0]][$titles[1]][]=$link;
> break;
> case 3:
> $dl[$titles[0]][$titles[1]][$titles[2]][]=$link;
> break;
> }
>
> This works pretty good as long as I expand the switch to have more cases
> than largest possible size for the titles array. However, I can't help
> feeling that there must be a better way, something which would custom
> generate the multidemensional array each time I insert data into it.
>
> This is rediculous, but it might give you the idea of what I am looking
> for:
> foreach($titles as $title){
> $dl.="[$title]";
> }
>
> $dl[]=$link;
>
> Is this possible in PHP ?
>
> red


http://www.php.net/foreach

C.J.
Reply With Quote
  #3 (permalink)  
Old 08-12-2004
Michael Fesser
 
Posts: n/a
Default Re: dynamically generating a multidemensional array

.oO(Red)

>This is rediculous, but it might give you the idea of what I am looking
>for:
>foreach($titles as $title){
>$dl.="[$title]";
>}
>
>$dl[]=$link;
>
>Is this possible in PHP ?


Yep.

1) With eval(), but you don't really want this (too slow):

$key = '';
foreach ($titles as $title) {
$key .= "['$title']";
}
eval("\$dl{$key}[] = \$link;");


2) With a function that iterates over the titles and creates the nested
array structure on-the-fly, looks more complicated than eval(), but is
faster:

setValue($dl, $titles, $link);

function setValue(&$tree, $key, $value) {
$current = &$tree;
for ($i = 0; $i < count($key); $i++) {
$subkey = $key[$i];
// create subtree if it doesn't exist yet
if (!isset($current[$subkey])) {
$current[$subkey] = array();
}
// move one level further down the tree
$current = &$current[$subkey];
}
// finally store the value
$current[] = $value;
}

HTH
Micha
Reply With Quote
  #4 (permalink)  
Old 08-12-2004
Chung Leong
 
Posts: n/a
Default Re: dynamically generating a multidemensional array


"Red" <groups@reenie.org> wrote in message
news:YxzSc.21613691$Of.3600283@news.easynews.com.. .
> I have an array which is dynamically generated by parsing a web page:
> titles Array
> (
> [0] => bookmarks
> [1] => New Folder
> [2] => New Folder2
> )
> and I want to insert html links into another, nultidemensional array
> which is based on the first array:
> $dl[$titles[0]][$titles[1]][$titles[2]]=$link;
>
> However this only works if the titles array has exactly 3 members.
>
> So I made this switch:
> $num=sizeof($titles);
> switch($num){
> case 1:
> $dl[$titles[0]][]=$link;
> break;
> case 2:
> $dl[$titles[0]][$titles[1]][]=$link;
> break;
> case 3:
> $dl[$titles[0]][$titles[1]][$titles[2]][]=$link;
> break;
> }
>
> This works pretty good as long as I expand the switch to have more cases
> than largest possible size for the titles array. However, I can't help
> feeling that there must be a better way, something which would custom
> generate the multidemensional array each time I insert data into it.
>
> This is rediculous, but it might give you the idea of what I am looking
> for:
> foreach($titles as $title){
> $dl.="[$title]";
> }
>
> $dl[]=$link;
>
> Is this possible in PHP ?
>
> red


Easy.

$dl = $link;
foreach(array_reverse($titles) as $title) {
$dl = array( $title => $dl );
}


Reply With Quote
  #5 (permalink)  
Old 08-12-2004
Red
 
Posts: n/a
Default Re: dynamically generating a multidemensional array

Michael Fesser wrote:

> .oO(Red)
>
>
>>This is rediculous, but it might give you the idea of what I am looking
>>for:
>>foreach($titles as $title){
>>$dl.="[$title]";
>>}
>>
>>$dl[]=$link;
>>
>>Is this possible in PHP ?

>
>
> Yep.
>
> 1) With eval(), but you don't really want this (too slow):
>
> $key = '';
> foreach ($titles as $title) {
> $key .= "['$title']";
> }
> eval("\$dl{$key}[] = \$link;");
>
>
> 2) With a function that iterates over the titles and creates the nested
> array structure on-the-fly, looks more complicated than eval(), but is
> faster:
>
> setValue($dl, $titles, $link);
>
> function setValue(&$tree, $key, $value) {
> $current = &$tree;
> for ($i = 0; $i < count($key); $i++) {
> $subkey = $key[$i];
> // create subtree if it doesn't exist yet
> if (!isset($current[$subkey])) {
> $current[$subkey] = array();
> }
> // move one level further down the tree
> $current = &$current[$subkey];
> }
> // finally store the value
> $current[] = $value;
> }
>
> HTH
> Micha

Wow. It took me two days to figure out what I needed, I don't think I
could ever have figured that out. Thanks, it works great.

red
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:52 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0