SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,


    I'm attempting to populate my associative arrays dynamically, does anyone know if this is possible please?

    e.g. I'd like the associative array

    delimeter["dynamically_created"][1] = stuff;


    but, writing:

    $dynamically_created = "stuff";
    $delimiter[$dynamically_created][1] = "stuff";

    doesn't seem to do it?

    Do you know how to do this please?

    I've noticed it's poss. to create them dynamically if I use integers, but I'd like to use strings.

    Cheers,

    Jason

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand: don't you have to use integers to number the arrays? I do recall something about arrays with text values instead of integers...am I imagining that?

    I'll go look this up in my big old red PHP book and see if I can figure it out.

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I don't see the problem.

    Checkout these two blocks of code:

    Code:
    <?php
    $dynamically_created = "stuff";
    $delimiter[$dynamically_created][1] = "stuff";
    echo($delimiter[$dynamically_created][1]);
    ?>
    Now this one...

    Code:
    <?php
    $dynamically_created = "stuff";
    $delimiter[$dynamically_created][1] = "stuff";
    echo($delimiter["stuff"][1]);
    ?>
    Both of those print the word "stuff" onto the page. Could you elaborate on your problem a bit, because I don't see any problem. I may be misunderstanding you, however.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not misunderstanding!


    Sorry mate, the problem was fatigue I think....!!!


    Cheers,

    Jase

  5. #5
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Commish,
    arrays can be indexed using numbers, text or a combo.

    When text is used they are called associative arrays.
    It is not uncommon to have soemthing like this.

    $links[1][title]="A LINK";
    $links[1][desc]="This is a link!..";
    $links[2][title]="A LINK2";
    $links[2][desc]="This is a link descritpion for link2!..";
    foreach($links as $link){
    echo '<B>'.$link[title].'</b><BR>';
    echo '<font size=1>'.$link[desc];
    }

    Messy code (its time for bed!) but you get the idea...
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  6. #6
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also you could have the following

    $array[1][1][1][2][4]="hi";

    if you really were so inclined...i dont think there is any limit in PHP...not to sure on that though, and i will leave it to freddy to delve deep into the langaueg and enlighten us regards how many keys an array value can have...from me its goodnight i will see you in 12 hours!.
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yes, multi-dimensional arrays, right? Sort of like a mini-database if you just want to store a little bit of data.

    And no problem Jase: I know what you mean. Glad to have helped!

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing I have to add is that you can specify the key for an array anyway you like.

    Whether you do it

    $dynamically_created = "stuff";
    $delimiter[$dynamically_created][1] = "stuff";

    $delimiter["stuff"][1] = "stuff";

    $delimiter = array("stuff" => array(1 => "stuff"));

    printing $delimiter["stuff"][1] will

    print

    stuff

    inall cases
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •