RE: [PHP] trouble with each() list() and array()

From: Date: Mon, 20 Nov 2000 19:42:10 +0000
Subject: RE: [PHP] trouble with each() list() and array()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-26319@lists.php.net to get a copy of this message
Christian Dechery ... > At 11:15 20/11/00 +0100, you wrote: > > (blah, blah, blah) > > well... thanks a lot, now I got it! You can explain much better than the > manual. Perhaps i could modify the manual!! ;-) > Is hard for me to "forget those damned poniters" since in C, > pointers are everything. It's hard to let them go. Think this way: In C, you must do the right things to improve the performance of your program, and you must take care of almost *EVERYTHING*... In PHP, you must hurry in order to get that website ready for Tuesday... No matter how slow it is, no matter how many memory the server will spend, no matter anything... > Now I got what an array is, and what list and each does. But somethings are > still amiss. Array's are not dynamic are they? I can't fill an array within > a loop right? like in C > > for(i=0;i<50;i++) > vet[i]=something; > > I can't feed an array in PHP right? You CAN!! In PHP there is something called "array pointer" that ""points"" (forget that C++ pointers!) to the "active" position of the array. the each() function, for example, advances the array pointer one place. But the best way to make large arrays of consecutive numbers is: for(i=0;i<50;i++) vet[]=something; note the vet[] , there is nothing between []!! That way, PHP itself will allocate a new element into the array, that will be the last number plus one. Really easy, i think. You don't have to worry how many elements the array has up to the moment, no matter what the last element is... > and how do I create a list in memory? Like, if I have a text file with each > line containing > name|lastname|email > > in C I'd create a struct called X > typedef struct X > { > char *name; > char *lname; > char *email; > } > > and just loop around the file until EOF feeding a list of X, right? I > didn't quite get how to do things like that in PHP. > The same problem with binary files, how do I read them without structs?? Just make multi-dimensioned arrays, like this for example... $friends[1][name]="John"; $friends[1][email]="john@domain.com"; $friends[2][name]="Mary"; $friends[2][email]="mary@anotherdomain.com"; and so on. You can also do it the reverse way, $friends[name][1]... Sure you got it. Ah, if you use separators like "|" in your files, you should look the explode() function. It's so funny and easy to separate text!!! And for those binary files... just don't use binary files. Or do you care how many hard-disk space is used in that webserver with 200 Gb ?? :-D > I hope those aren't stupid doubts. No, never mind... perhaps they smell a bit to newbie questions, but i'm happy to help :-) Also, if you're coming from C, the arrays in PHP could look dificult, and i can see that.

« previous php.general (#26319) next »