471,083 Members | 1,123 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,083 software developers and data experts.

dynamically generating a multidemensional array

Red
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
Jul 17 '05 #1
4 1695
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.
Jul 17 '05 #2
.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
Jul 17 '05 #3

"Red" <gr****@reenie.org> wrote in message
news:Yx***********************@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 );
}
Jul 17 '05 #4
Red
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
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.