Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-14-2005, 01:37 PM   #1 (permalink)
phill2000star
Registered User
 
Join Date: Jul 2005
Posts: 4
phill2000star is on a distinguished road
Using vars to create an array??

Hiya,

Is this possible?

I want the user to submit a number and to create a number of arrays equal to that number, each of them including that an icrimental number in the name of the array.

EG.

User submits the number 4 which is asigned to the variable $noOfArrays. This should create the following arrays -

array1[]
array2[]
array3[]
array4[]

I thought about doing a loop to produce each of the arrays, but I'm stuck. for example i thought about using the following code, but dont know any other way of doing it.

Code:
for ($i = 0; $i < $noOfArrays ; $i++) {
array.$i = array();}
Does anyone understand me? i was trying to use the . to join the word array and the variable $i to creat the following - array0[] array1[] array2[] and array3[].

I am doing something wrong but for the life of me cant decide how else to do it??....

Any ideas please!?!?

Many thanx!!
phill2000star is offline   Reply With Quote
Old 07-14-2005, 03:08 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,471
sde is on a distinguished road
sure, you couldmake an array of arrays.. i think you are on the right track , just need a little help with the syntax. maybe try this:
PHP Code:
<?
for($i=0;$i<$noOfArrays;$i++){

  
// now just make a new array
  
$myArrayOfArrays[] = array();

}
?>
$myArrayOfArrays is a container for all your arrays that were created. They are all empty arrays, but they do exist.

hope that helps.
__________________
Mike
sde is offline   Reply With Quote
Old 07-14-2005, 03:11 PM   #3 (permalink)
phill2000star
Registered User
 
Join Date: Jul 2005
Posts: 4
phill2000star is on a distinguished road
hiya.

Thanx for the reply, but what im really after is to join a fixed variable such as the word "array" combined with a second variable which will be a number, and then use that to create an array.

for eaxmple...


array.1[]
array.2[]
array.3[] etc

I cant think of any way to explain it!!...

You know you can join two strings together? well i want to use the resulting string as a name for a new array?...

Does that help?...
thanx for your reply!

Phil
phill2000star is offline   Reply With Quote
Old 07-15-2005, 07:07 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,471
sde is on a distinguished road
hmm .. i thought that is what arrays were for. i've never really dynamically named variables, but there might be a way.

i'm curious as to the reason you need it that way, instead of just making it an array itself.

array[0]
array[1]
array[2]
array[3]

etc.. if you make it an array, it will be much easier to manage with php as well.
__________________
Mike
sde is offline   Reply With Quote
Old 07-16-2005, 08:37 AM   #5 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Yeah, I'm not understanding the reason either, but take a look at variable variables.

-r
idx is offline   Reply With Quote
Old 07-17-2005, 01:36 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,695
redhead is on a distinguished road
I'm guessing the issue here is from, what I can remember, bad coding style learned during the VB/BASIC days, where you would say, "Hey I need to dynamicaly use some variable.. Well lets just create what we want:, ie:
PHP Code:
$my_hey "this is hey";
$my_yo "this is yo";
function 
give_me($decide){
    return ${
"my_$decide"};

Which could easily be changed to:
PHP Code:
$myArray = array(
         
"hey" => "this is hey",
         
"Yo"  => "this is Yo");
function 
give_me($decide){
    return 
$myArray[$decide];

For whatever reason I frequently see old BASIC programmers using loops with referencing variables like that, where they just append the current index to teh variable name, instead of just looping through an array..

For readability, and maintainability, I would rather have it in an array where the code is kinda self explaining, instead of some giberish someone would have to rewrite 5 years from now, because it's just poor programming style.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 07:59 AM
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 12:14 PM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM


All times are GMT -8. The time now is 04:08 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting