Page 1 of 1

PHP operators

Posted: Thu Jun 09, 2005 6:52 am
by jimbo9
I have just started to lean PHP and my manual "PHP and MySQL Development" by Welling & Thomson shows the following code re a multidimensional array:

Code: Select all

$products = array( array( 'Code' => 'Tire',
                          'Description' => 'Tires',
                          'Price' =>100
                         ),
etc...

There is absolutely NOTHING I can find that explains exactly what the operator "=>" does. Can anyone please tell me as it keeps cropping up in oscommerce too!

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]

Posted: Thu Jun 09, 2005 6:57 am
by John Cartwright
Its so you can add a key to a value.

Code: Select all

$products = array('chair','stamp','nut');

echo $product[0]; //chair
echo $product[1]; //stamp
would now be with a key

Code: Select all

$products = array('chair' => 'brown','stamp' => 'red','nut' => 'purple');

echo $product['chair']; //brown
echo $product['stamp']; //red
This way you can now handle things a lot easier. Especially in loops

Code: Select all

$products = array('chair' => 'brown','stamp' => 'red','nut' => 'purple');

foreach ($products as $key => $value) {
     echo $key .' - '. $value .'<br />'; 
}

Posted: Thu Jun 09, 2005 7:10 am
by jimbo9
Many thanks for swift reply. And I will use the PHP formatting next time!