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] => ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
.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 |
|
|||
|
"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 ); } |
|
|||
|
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 |