PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 8th, 2002, 01:41 PM
seiya seiya is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: NYC, NY, USA
Posts: 45 seiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to seiya
Advanced Arrays

Hi, I am learning PHP and I need some help with advanced arrays, anyone knows of a good tutorial on it?

Reply With Quote
  #2  
Old August 8th, 2002, 01:47 PM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Advanced Arrays

how bout this one?
http://codewalkers.com/tutorials.php?show=8&page=1

Reply With Quote
  #3  
Old August 8th, 2002, 01:48 PM
jorgen jorgen is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Hardenberg, Holland<marquee>
Posts: 284 jorgen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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

Reply With Quote
  #4  
Old August 8th, 2002, 01:50 PM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Advanced Arrays

yer, thats probly a better idea... RTFM!!

Reply With Quote
  #5  
Old August 9th, 2002, 01:13 AM
seiya seiya is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: NYC, NY, USA
Posts: 45 seiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to seiya
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...

Reply With Quote
  #6  
Old August 9th, 2002, 07:54 AM
siteworkspro.com siteworkspro.com is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 92 siteworkspro.com User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to siteworkspro.com Send a message via AIM to siteworkspro.com
RE: Advanced Arrays

and so you should!

Sleep is the beggining of all evil!

Reply With Quote
  #7  
Old August 9th, 2002, 04:23 PM
seiya seiya is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: NYC, NY, USA
Posts: 45 seiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to seiya
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!

Reply With Quote
  #8  
Old August 9th, 2002, 04:26 PM
seiya seiya is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: NYC, NY, USA
Posts: 45 seiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to seiya
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!

Reply With Quote
  #9  
Old August 9th, 2002, 04:46 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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'.

Reply With Quote
  #10  
Old August 9th, 2002, 04:47 PM
postmoderngoblin's Avatar
postmoderngoblin postmoderngoblin is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: UK
Posts: 55 postmoderngoblin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 53 sec
Reputation Power: 2
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.


Reply With Quote
  #11  
Old August 9th, 2002, 05:05 PM
postmoderngoblin's Avatar
postmoderngoblin postmoderngoblin is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: UK
Posts: 55 postmoderngoblin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 53 sec
Reputation Power: 2
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.

Reply With Quote
  #12  
Old August 9th, 2002, 05:51 PM
seiya seiya is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: NYC, NY, USA
Posts: 45 seiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to seiya
RE: Advanced Arrays

Thanks, this has helped me really a lot. =)

Reply With Quote
  #13  
Old August 9th, 2002, 05:53 PM
postmoderngoblin's Avatar
postmoderngoblin postmoderngoblin is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: UK
Posts: 55 postmoderngoblin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 53 sec
Reputation Power: 2
RE: Advanced Arrays

Well I'm still fairly new and did not know that, cheers.
:eek: best remember it for future ref.

Reply With Quote
  #14  
Old August 10th, 2002, 12:33 AM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: Advanced Arrays

If you want to see what the assignments are doing in the array creation try doing this:
php Code:
Original - php Code
  1.  
  2. <?php
  3. //this is from user notes for print_r on php.net
  4. function dp($call,$cname) {
  5. // call: the variable you want to print_r
  6. // cname: the label for your debugging output
  7.     global $debug;
  8.     if ($debug) {
  9.         echo $cname.":<pre>";
  10.         if (!is_array($call)) { $call=htmlspecialchars($call); }
  11.         print_r($call);
  12.         if ( is_array($call)) { reset($call); }
  13.         echo "</pre><hr>";   
  14.     } // end function dp()
  15. $debug=true;
  16.  
  17. $books = array(0=>array('name'='A Book','price'=>9.99),1=>array('name'='Another Book','price'=>17.99));
  18. dp($books,'This is the $books array dump!');
  19. ?>


It's a very useful function for debugging...

Cheers,
Keith

Reply With Quote
  #15  
Old August 10th, 2002, 02:04 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: Advanced Arrays

ahhh ... arrays, gotta love 'em!
Maybe your a visual type of person, and this could further assist:
php Code:
Original - php Code
  1.  
  2.             'name'      'price'
  3. $books 0 [A Book      ] [ 9.99]
  4.        1 [Another Book] [17.99]


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: