473,671 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically generating a multidemensiona l 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, nultidemensiona l 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($ti tles);
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 multidemensiona l 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 1804
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, nultidemensiona l 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($ti tles);
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 multidemensiona l 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($title s 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($curren t[$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, nultidemensiona l 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($ti tles);
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 multidemensiona l 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_r everse($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($titl es 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($curren t[$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
5955
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 want this for speed. I know you can increment regular arrays via ++array or array++ etc. etc. but how do you do it with multidemensional arrays? lets say I have array and I need to access array how can I do this without the subscripts? The x and y...
1
1499
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 notation. I'm curious if a similar concept exists in Csharp. I'm building content management apparatus for ASP.NET pages. I was thinking it would be nice if I could take database content and generate objects out of the results of the query. The...
4
2174
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. I'm not using javascript to get data from database. I'm fetching this using php code. I used square brackets to declare it as array. Hope you understand my problem. Or else , i explain here clearly be patience to read this. In my design i'm...
3
2372
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 represents my menu system, and I would like to sort the third level menu items only and not the first two. So, I've been trying to use some of the suggestions I've found here about sorting. Mainly, I've created a comparison interface.
1
1893
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 existing JavaScript array I don't have any problems, but I'm generating the arrays dynamically, then populating my dropdown dynamically with the onload event. I've tried setting multiple encodings in the meta tag with no success.
2
4272
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
1244
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 telling me the size of the pointer myArray Many thanks G
1
1197
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 Array("DefaultArray1_1","DefaultArray1_2","DefaultArray2_1","DefaultArray2_2"); for (i=0; i < myarray.length; i++) { myarray = new Array(); myarray= ; }
9
1867
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 associate the call with the handling routine (for reference purposes, I'm calling the Google Language Translation API). As a work-around, I thought I'd dynamically generate a unique callback function for each API call. Right now, I'm stuck hobbling...
0
8481
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8602
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6234
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.