SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: dynamic arrays in php
-
Jan 16, 2001, 13:36 #1
- 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
-
Jan 16, 2001, 13:47 #2
- 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.
-
Jan 16, 2001, 13:51 #3
- 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]); ?>
Code:<?php $dynamically_created = "stuff"; $delimiter[$dynamically_created][1] = "stuff"; echo($delimiter["stuff"][1]); ?>
-
Jan 17, 2001, 05:03 #4
- 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
-
Jan 17, 2001, 06:35 #5
- 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!
-
Jan 17, 2001, 06:37 #6
- 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!
-
Jan 17, 2001, 08:30 #7
- 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!
-
Jan 17, 2001, 11:51 #8
- 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 casesPlease don't PM me with questions.
Use the forums, that is what they are here for.
Bookmarks