By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Best way to "switch arrays"

P: n/a
I'm defining several arrays in the following format:

$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')

I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.

Ideally, I may even have a different set of elements and then need to
create a variable on the fly to match the element name.

Can someone suggest the best method for changing the array to use,
without having to script a different loop for each section of the
file I'm stepping through?

Thanks.

J
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
javelin wrote:
I'm defining several arrays in the following format:

$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')

I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.
How do you "detect" the section?
You could have an array of arrays, keyed by a section name:
$animals['farm'] = array('horse', 'cow', 'chicken');
$animals['pets'] = array('dog', 'cat', 'bird');
$animals['zoo'] = array('monkey', 'lion', 'zebra'. 'giraffe');
Jun 27 '08 #2

P: n/a
javelin schrieb:
I'm defining several arrays in the following format:

$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')

I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.

Ideally, I may even have a different set of elements and then need to
create a variable on the fly to match the element name.

Can someone suggest the best method for changing the array to use,
without having to script a different loop for each section of the
file I'm stepping through?
I think, you know in wich section you are?

$actualarray = $farmanimals; // or $hose... or $zoo...
Now work with the $actualarray.

Jun 27 '08 #3

P: n/a
On Jun 13, 3:46 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
javelin wrote:
I'm defining several arrays in the following format:
$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')
I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.

How do you "detect" the section?
You could have an array of arrays, keyed by a section name:
$animals['farm'] = array('horse', 'cow', 'chicken');
$animals['pets'] = array('dog', 'cat', 'bird');
$animals['zoo'] = array('monkey', 'lion', 'zebra'. 'giraffe');
The file will have a header line at the start of each section. I check
for the section, then I want to "switch" to the next array
dynamically.

The code looks like this at this point, hopefully this will help:

$file = fopen('sampledata1.txt','r');
while (!feof($file)) {//step thru each line in file
$nextline = fgets($file, $currentArray['linelength']); //using the
currentArray
echo $nextline . "<br>";
if (strpos($nextline,"section2")) {
# somehow, change the $currentdata array to use the next
one in line. Need to use the
# same name, "currentdata", for the array name.
}
foreach ($currentdata as $col =$len){
# step thru the array elements, each which defines
the length of the next portion of the line
print substr(startofline, $currentdata[$len]) ."<br>";
}

}
Jun 27 '08 #4

P: n/a
On Jun 14, 7:37 am, javelin <google.1.jvm...@spamgourmet.comwrote:
On Jun 13, 3:46 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
javelin wrote:
I'm defining several arrays in the following format:
$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')
I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.
How do you "detect" the section?
You could have an array of arrays, keyed by a section name:
$animals['farm'] = array('horse', 'cow', 'chicken');
$animals['pets'] = array('dog', 'cat', 'bird');
$animals['zoo'] = array('monkey', 'lion', 'zebra'. 'giraffe');

The file will have a header line at the start of each section. I check
for the section, then I want to "switch" to the next array
dynamically.

The code looks like this at this point, hopefully this will help:

$file = fopen('sampledata1.txt','r');
while (!feof($file)) {//step thru each line in file
$nextline = fgets($file, $currentArray['linelength']); //using the
currentArray
echo $nextline . "<br>";
if (strpos($nextline,"section2")) {
# somehow, change the $currentdata array to use the next
one in line. Need to use the
# same name, "currentdata", for the array name.
}
foreach ($currentdata as $col =$len){
# step thru the array elements, each which defines
the length of the next portion of the line
print substr(startofline, $currentdata[$len]) ."<br>";
}

}
Well, I realized that the best way to deal with this is creating an
array of the arrays, returning it as a function. Then, I can easily
change arrays by referring to the index (I gave each array rec an
associated number that I can easily relate to my data).

Thanks for the help.
J
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.