473,388 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1788
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hendrix | last post by:
Hi, I would like to pick four elements from a multidemensional array and use the content of those elements. I want to do this without all the subscript calls required for individual access, just...
1
by: | last post by:
In Actionscript 2.0 there is the concept of dynamically generating and naming objects at runtime. Dynamically named objects can be referenced and manipulated programmatically using array-access...
4
by: somaskarthic | last post by:
Hi I posted query about dynamically generating php code. Sorry for the inconvenience . I didn't given you the php code. Its just javascript code which dynamically create rows on button click event....
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
1
by: jeffejohnson | last post by:
I'm looking to see if anyone has experienced this... I've got a dropdown that I'm populating dynamically and the items include HTML special characters (like &Ocirc;). If I load them from an...
2
by: Perseus | last post by:
Hi To split a single string we do the following # myString= "bannana, bowling balls, juice" string singleArray= myString.split(','); # This gives singleArray = "bannana" etc.
2
by: GraemeC | last post by:
How can you determine the length of a dynamically created multidimensional array? Tried using sizeof unsuccesufully. It always returns 4, presumably becuase I am using ints and it is just...
1
by: rupak | last post by:
I want to creat new list of an arrays whoes name are defined from an arry it self. Thus generating arrays dynamically from an array. Ex: var myarray=new...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.