|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Advanced Arrays
Hi, I am learning PHP and I need some help with advanced arrays, anyone knows of a good tutorial on it?
|
|
#2
|
|||
|
|||
|
RE: Advanced Arrays
how bout this one?
http://codewalkers.com/tutorials.php?show=8&page=1 |
|
#3
|
|||
|
|||
|
RE: Advanced Arrays
Well, you can download the manual from php.net
There's a good tutorial about array's www.php.net/docs.php |
|
#4
|
|||
|
|||
|
RE: Advanced Arrays
yer, thats probly a better idea... RTFM!!
|
|
#5
|
|||
|
|||
|
RE: Advanced Arrays
Thank you all... I will make my questions and mayeb someone could help... I will have them ready tomorrow or in 2 days... i am not in the mood to study now...
|
|
#6
|
|||
|
|||
|
RE: Advanced Arrays
and so you should!
Sleep is the beggining of all evil! |
|
#7
|
|||
|
|||
|
RE: Advanced Arrays
Alright, here is what i need help with:
<?PHP $books = array(0=>array('name'='A Book','price'=>9.99),1=>array('name'='Another Book','price'=>17.99)); ?> Why 0=>array ? and 1=>array? Also, => , Why do you use it? I hope someone could help me, thanks in advance! |
|
#8
|
|||
|
|||
|
RE: Advanced Arrays
Hmm.. I also need help with this code from the tutorial...
<?PHP echo $books[0]['name']; echo $books[1]['price']; $books[0]['price'] = 12.99; foreach($books as $onebook) { echo $onebook['name'] . " sells for $". $onebook['price'] . "<BR>n"; } ?> Sorry! |
|
#9
|
|||
|
|||
|
RE: Advanced Arrays
0 => and 1 => are implicit in fact.
They are indexes of your array. When declaring an array $foo = Array( 'A', 'B' ) for example, you have now $x[0] being 'A', while $x[1] being 'B'. The => operator is used to assign a value to an index Now if you declare $arr = Array( 'Foo' => 'A', 'Bar' => 'B' ) you have now $x['Foo'] being 'A', while $x['Bar] being 'B'. |
|
#10
|
||||
|
||||
|
RE: RE: Advanced Arrays
Why 0=>array ? and 1=>array?
This is what is called a multidimensional array - you could think of it a bit like a table but that would screw with your head when you start thinking about sub-sub-arrays - which is a bit 3D in effect and beyond. Ordinarily an index of an array, in this case the '0' or '1' is linked with and used to find only one piece of data. By using 1=>array you can sort of cheat by saying the one bit of data I'm linked with is an array, which can hold lots of stuff. although the best reason is that in this way you don't have to name every single (sub)array. Which is nice. Also, => , Why do you use it? Its just a way of expressing the link between two bits of data. As an aside: You should look at objects because you can do just the same sort of stuff with them, you might find it a bit easier to read and figure out whats going on and they can be more flexible. |
|
#11
|
||||
|
||||
|
RE: RE: Advanced Arrays
<?PHP
// output element '0' of books, which is an array. // from that array output element 'name' echo $books[0]['name']; // output element '1' of books, which is an array. // from that array output element 'price' echo $books[1]['price']; //Assign to element '0' of books, // the information that price => 12.99 $books[0]['price'] = 12.99; // for each individual element of books //starting from the beginning // refer to that element as $onebook foreach($books as $onebook) { //output the details of this element of $books echo $onebook['name'] . " sells for $". $onebook['price'] . "<BR>n"; } // repeat until reaching the end of $books ?> It just so happens that the details of each element of $books also happens to be an array so you need to index into that using 'price' and 'name' to get the info that you want. The great thing about this is if you want to use irregular numbers : [1] => "boo" [13] => "hoo" [197] => "um" or strings ["scooby"] => "boo" ["dooby"] => "hoo" ["doo"] => "um" foreach will still check every element doing whatever checks or output you told it to do. Other methods of checking through an array won't neccessarily do this. |
|
#12
|
|||
|
|||
|
RE: Advanced Arrays
Thanks, this has helped me really a lot. =)
|
|
#13
|
||||
|
||||
|
RE: Advanced Arrays
Well I'm still fairly new and did not know that, cheers.
:eek: best remember it for future ref. |
|
#14
|
|||||
|
|||||
|
RE: Advanced Arrays
If you want to see what the assignments are doing in the array creation try doing this:
php Code:
It's a very useful function for debugging... Cheers, Keith |
|
#15
|
|||
|
|||
|
RE: Advanced Arrays
ahhh ... arrays, gotta love 'em! Maybe your a visual type of person, and this could further assist: Think of $books as being a table, which stores records ... like a db. Using the above example, there are a total of 2 records (you can find this out by using sizeof($books)). Now for each record, we are going to store 2 properties/fiedls ... which we have named "name" and "price". So when we want to retrieve the price of the first record we would do this: $books[0]['price'] Fill the array with data. php Code:
|