Connecting Tech Pros Worldwide Help | Site Map

more about arrays

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:19 AM
deko
Guest
 
Posts: n/a
Default more about arrays

Is it possible to read a multi-dimensional array from a file? How would one
represent a multi-dimensional array in a flat file? Is this possible? For
example, I want to have 3 elements in the array, each of which is an array
itself:

Element 1
ARRAY (1-4)
Element 2
ARRAY (1-120)
Element 3
ARRAY (1-1460)

I'd like to store this array in a delimitated flat file - all integers.

3|84|753|2|0|349|5|0| ... and so on

perhaps I need 2 files? other options? (I know... get a database...)



  #2  
Old July 17th, 2005, 04:19 AM
Phil Roberts
Guest
 
Posts: n/a
Default Re: more about arrays

With total disregard for any kind of safety measures "deko"
<dje422@hotmail.com> leapt forth and uttered:
[color=blue]
> Is it possible to read a multi-dimensional array from a file?
> How would one represent a multi-dimensional array in a flat
> file? Is this possible? For example, I want to have 3 elements
> in the array, each of which is an array itself:
>
> Element 1
> ARRAY (1-4)
> Element 2
> ARRAY (1-120)
> Element 3
> ARRAY (1-1460)
>
> I'd like to store this array in a delimitated flat file - all
> integers.
>
> 3|84|753|2|0|349|5|0| ... and so on
>
> perhaps I need 2 files? other options? (I know... get a
> database...)
>
>
>[/color]

Maybe you should consider looking at serialize()

--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
  #3  
Old July 17th, 2005, 04:20 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: more about arrays

deko wrote:[color=blue]
> Is it possible to read a multi-dimensional array from a file? How would one
> represent a multi-dimensional array in a flat file? Is this possible? For
> example, I want to have 3 elements in the array, each of which is an array
> itself:
>
> Element 1
> ARRAY (1-4)[/color]
Do you mean 0-3? :)
[color=blue]
> Element 2
> ARRAY (1-120)
> Element 3
> ARRAY (1-1460)
>
> I'd like to store this array in a delimitated flat file - all integers.
>
> 3|84|753|2|0|349|5|0| ... and so on
>
> perhaps I need 2 files? other options? (I know... get a database...)[/color]

If each element is separated by "|" and each array separated by newlines
I'd try this:

<?php
$lvl1 = explode("\n", file($filename));
// $lvl1[0] contains the first line of $filename
// $lvl1[1] contains the second line
// ...

foreach ($lvl1 as $k=>$line) {
$lvl2[$k] = explode("|", $line);
}
// $lvl2[0] is an array
// $lvl2[0][0] contains the first integer in the first line
// $lvl2[0][1] contains the second integer in the first line
// ...
// $lvl2[1][0] contains the first integer in the second line
?>
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.