More array learning for me.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Still struggling with arrays I'm afraid. So hopefully can turn the
light on for me.

So if I wanted to make a multi dimensional array from a text file how
would I do it?

I can work out how to make an array.


$file_handle = fopen("myfile.txt", "rb");

while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$data = explode('=', $line_of_text);

print $data[0] . $data[1]. "<BR>";



With the txt file dataa = data b = datac = datad ......

But I would like to make an array

$data = array(
        'url' => "http://myurl",
        'alt' => "my image",
        'title' => "my title"
        'link' => "my link',
        'url' => "http://myurl",
        'alt' => "my image",
        'title' => "my title"
        'link' => "my link',
        'url' => "http://myurl",
        'alt' => "my image",
        'title' => "my title"
        'link' => "my link',

But I just cannot work out how.

Re: More array learning for me.


Quoted text here. Click to load it

Just a hint: You don't really need fopen/fgets/fclose if you want to
read an entire file, file() and file_get_contents() exist.

Quoted text here. Click to load it

You should post some real example data. Given the above all I can do is
guessing. Does each sub array represent a line of your file? If that's
the case, read the file with file() into an array. Then you can loop
through it and use explode() to create the sub arrays. Another option
would be something like array_walk() instead of the loop.

As said - with some more details and the result you want to get from it
it's easier for use to post some better hints and maybe example code.


Re: More array learning for me.

On Feb 23, 1:52=A0pm, ""
Quoted text here. Click to load it


Preface: Should you be using a database for this application?

Okay, now moving on for the sake of answering the question.  How you
make a two dimensional array depends on a number of factors:

1.  Are you also writing to this file, or is it being provided from
another source with a pre-defined format?

If I was the sole maintainer of this file, I'd make my life easier and
just use serialize() and unserialize().

2. What is the format of the text file?  Your example is not very

[re-ordered snips follow]

Quoted text here. Click to load it

So you now have the equivalent to:

$data = array("dataa ", " data b ", " datac ", " datad ");

I can't imagine that that's what you want or how you really intend to
split the data.  Furthermore, you say you want this format:

Quoted text here. Click to load it

And yet, I don't see any of the fields represented in the data file
snippet you provided.

Finally, the only delimiter information you've given us is '=3D', which
is equally (no pun intended) valid in any one of the four fields
you're trying to extract.  If the fields are properly delimited, there
may be nothing you can do.  Again, this is all speculation until you
reveal the format of the data file to us...

Re: More array learning for me.

On Feb 23, 2:22=A0pm, matt unfortunately wrote:

Quoted text here. Click to load it

That is, if the fields are NOT properly delimited...

Re: More array learning for me.

Quoted text here. Click to load it

Blimey Matt, I am impressed by the speed and depth of your response. I
have learnt quite a bit from that.

I think you are correct that I am probably better doing this with a
database. The plan was to display a number of images of templates
along with their descriptions and a link to their live demo. For ease
of others adding new templates I was thinking of a text file but
thinking more about it a simple form writing to a database will
probably be a lot easier. Then all I have to do is read from that
database into an array and create the html from that (a simple <li></
li> that then displays the image and data using Jquery). Would that be
a better method?

Site Timeline