dynamically generating a multidemensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Red

    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, 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
  • C.J. Garibay

    #2
    Re: dynamically generating a multidemensiona l array

    Red wrote:
    [color=blue]
    > 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[/color]



    C.J.

    Comment

    • Michael Fesser

      #3
      Re: dynamically generating a multidemensiona l array

      .oO(Red)
      [color=blue]
      >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 ?[/color]

      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

      Comment

      • Chung Leong

        #4
        Re: dynamically generating a multidemensiona l array


        "Red" <groups@reenie. org> wrote in message
        news:YxzSc.2161 3691$Of.3600283 @news.easynews. com...[color=blue]
        > 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[/color]

        Easy.

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


        Comment

        • Red

          #5
          Re: dynamically generating a multidemensiona l array

          Michael Fesser wrote:
          [color=blue]
          > .oO(Red)
          >
          >[color=green]
          >>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 ?[/color]
          >
          >
          > 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[/color]
          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

          Comment

          Working...