I don't get arrays! HELP ME PLEASE!
| From: | Christian Dechery | Date: | Tue, 21 Nov 2000 20:30:04 +0000 |
| Subject: | I don't get arrays! HELP ME PLEASE! | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-26512@lists.php.net to get a copy of this message | ||
At 11:08 21/11/00 +0100, you wrote:
Christian Dechery ...ok... got it! You know what my real problem is? Addiction to C and it's nasty thousands of lines to do something simple. Like, I didn't get REALLY what that command array() is for. What can u do with it? Create and fill up a hash table? With what? for instance (I know I'm being a pain in the ass, just tell me when to stop and I will)... I created in pure C a pseudo-e-commerce system for my website. Since we don't have databases (lack of cash) I did it all with binary files. It works fine... perfect. And I have a logfile so I can create stats. It is in the format: product_id|product_name|time|boolean type(can be 1 or 0, 1 for click, 0 for view) ok? and I had to make a nice and cool log analyzer for the "clients" so they can view how their products are going. So I went there created structs, lists and tables in memory to store all the shit I needed and then put it out on nice and kewl templates... and I have NO idea how to build lists and tables on PHP... this array stuff isn't really giving me what I need. Or it does, but I can't see it. All the examples I've seen tells me nothing. and that is a low-to-medium complexity code, so I tought, if I could get that "translated" to PHP, than I'd undestand those DAMNED arrays... you know? I'm a pointer freak. I don't see three or four varibles coming from one function without pointers. sorry once again... I don't know anyone who programs in PHP, they all say: "PHP? Humpf... ASP rules.", and getting started from a manual isn't easy at all. ____________________________ . Christian Dechery (lemming) . http://www.tanamesa.com.br . Gaita-L Owner / Web DeveloperJust 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";cool... so If I want to feed a list I can just do that: while(fgets($str,lenght,file)) { code ... break $str ... code... $friends[][name]=$name; $friends[][email]=$email; } right? NO!! That would generate the following: $friends[1][name]=John $friends[2][email]=john@domain.com $friends[3][name]=mary $friends[4][email]=mary@anotherdomain.com Every time that you use [], a new element is created into the array!!! But if you use the other array i purposed, first email or name and then the number: $friends[name][] =$name $friends[email][]=$email That should work, as every time you do [], you do it in two separate arrays... with [] you allocate space for a new name, then for a new email... In the former example, you were allocating space for a new friend... and I should forget about structs and start using DBs? After all that's what DBs are for. cutting to the chase... there are no pointers in PHP, no structs. And this is due it's ease to deal with DBs, correct? Yeah. Using DBs in PHP is also pretty simple. I didn't know anything about SQL databases two months ago, and now i'm working with an impressive oracle8 database with more than 60 tables!! Dealing with databases is pretty simple. Connect to the database with ONE LINE of code, execute an statement with TWO LINES, and fetch the next record with ONE LINE of code... Every time i look at the code of a Java Servlet that deals with databases i can't help laughing... 5 lines to connect and instance a connection and an alias, another 4 or 5 to execute an statement, another 3 to fetch the next record... And of course, in Java you don't have that readable 20-chars-long lines of code... you have: document.all.layers.all.form1.inputbox.content = document.all.layers.all ...... and so on. I hate Java anyway.