Please help me get my head around multidimensional arrays in PHP.
-
Please help me get my head around multidimensional arrays in PHP. I'm trying to create an array that will store phone numbers. Each number has a group of attributes including: * Type - (i.e. Mobile, home, etc) * Number - (the actual number itself) * Extension (will frequently be null) * Is Preferred - (is this their primary contact number) I would like to be able to create this array, and access it via named indexes the same way I access a multi-column array that I pull out of a MySQL database. In fact, my ultimate goal is to check this data and insert it into a MySQL table when I don't know in advance how many rows of data I'll be working with. I understand that the syntax will be different (probably using a foreach or list or someting), but basically: while ($row = mysql_fetch_array ($Phone_Numbers, MYSQL_ASSOC)) { echo $Phone_Numbers['type'] . $Phone_Numbers['number'] . $Phone_Numbers['ext'] . $Phone_Numbers['preferred'] ; } I've tried various ways of creating and accessing this array, but none have worked quite like I'd hoped. It seems that either I get an array where I'm overwritting the values of each index with each row of data I try to put in, or I get an array that is so deep and complex that the data is in there, but I don't know how to retrieve it. So, given: $phoneType1, $phoneNumber1, $phoneExt1, $phonePreferred1 --AND-- $phoneType2, $phoneNumber2, $phoneExt2, $phonePreferred2 --AND-- [...] How do I create the array, and how do I retrieve data from the array by index?
-
Answer:
I agree with jon_kill. You want to do something like this (untested): class PhoneNumber { var $type; var $number; // ... } $numbers = array(); $numbers[0] = new PhoneNumber(); $numbers[0]->type = "mobile"; $numbers[0]->number = "555-1212"; $numbers[1] = new PhoneNumber(); $numbers[1]->type = "fax"; $numbers[1]->number = "123-4567"; // ... foreach ($numbers as $n) { echo $n->type . $n->number; } If you want a real working example, check out http://cvs.sourceforge.net/viewcvs.py/audacity/htdocs/include/news.inc.php?rev=1.3&view=auto and http://cvs.sourceforge.net/viewcvs.py/audacity/htdocs/index.php?rev=1.23&view=auto from a PHP web site I once wrote.
willnot at Ask.Metafilter.Com Visit the source
Other answers
I have a web page form where the user will enter their phone number(s). The page will start out with a few rows, and if the user has additional numbers they want to enter, the form will create new rows of form inputs. So, ultimately, the page does know how many numbers it's dealing with which I can assign as a variable, but I don't know how many rows of inputs I'll need to iterate through. So basically, the page woudl look like Preferred: type [ ....] number________ ext ___ Alternate type [....] number_________ ext ___ type [....] number_________ ext ___ [submit] I need to be able to check each number and insert it into a database. I'm not dead set on doing it with any particular method. I will definitely try what mbrubeck has suggested. Thanks
willnot
Not knowing about hashes in PHP is kind of like asking, "I'm using this hammer, but I put in a nail in a place where I don't want it. I'd like to remove it, but don't know how. I have seen this claw thing on the other end of the hammer, but I haven't read about that yet." In other words... get thee to http://www.php.net/manual/en/ or to a bookstore. You will find that the time you invest in reading is more than made up for by not having to go down the road of implementing everything "the hard way" and suffering along with the resulting bad code.
Rhomboid
Well, yes, but to many people (esp. Perl programmers) 'hash' and 'associative array' are synonymous. (Actually, I don't like the fact that you can do what you describe in PHP -- I'd rather see associative arrays done as real hashes, with the extra speed that provides.) /derail
littleme
Hmn. Forgot to use LT and GT tags, but you get the idea. Sorry about the foobed code, it looked fine in preview.
SpecialK
littleme: In PHP, hashes are known as "Associative Arrays." PHP arrays are really convenient in that you can access them both by the key index and by the name index. example: $array['foo'] = bar; echo $array[0].' '; echo $array['foo']; ?> > will print bar bar
SpecialK
Just to mention: Hankins' form code won't work exactly as shown, because he's trying to interpolate variable names within a single-quoted string, which doesn't work. Not difficult to fix, though.
littleme
I have no idea what hashes are Ever so slightly off-topic, but you *must* learn about hashes! They are immensely useful for all sorts of real-world programming situations. And they're really not difficult if you understand arrays. Basically, a hash is just a different type of array where, instead of the indexes being numbers (the 0th element, the 1st element, the 99th element etc), they are strings. So you can say "Give me the value in the 'type' element", "Give me the value in the 'number' element" and so on. If nothing else, it saves you having to remember that element 0 is the type, element 1 is the name, element 2 is the number etc.
littleme
mbrubeck - solution worked. ajbattrick and Hankins - I didn't realize you could string those[] index names together like that. So Hankins - it seems like your solution saves me the step of reading the various $_POST variables and then putting them into the array or class or whatever. For that reason, I'm assuming your method would be the preferable way to do it in this case I'll test it and make sure, but just so I understand better, when would I want to use a class instead? And Jon_Kill - no doubt, I still have much to learn. if you have any books or web pages you'd like to direct me towards, that would be helpful. Thanks
willnot
Related Q & A:
- I cannot get the toolbar with my favorites to appear...please help.Best solution by Yahoo! Answers
- I did something really bad and now i need help please help me.Best solution by Yahoo! Answers
- Can anyone PLEASE help me get into my e-mail?Best solution by Yahoo! Answers
- Help with TV show, please help?Best solution by Yahoo! Answers
- How to get rid of nasal allergies, please help?Best solution by getridofthings.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.