Associative Array Syntax

Submitted by:James Anderson

Date added:01 February, 2011

Category:PHP

Associative Array Syntax

Tags: php array , array syntax , array in php

Code Snippet:

/*Simple*/
$carParts = array(
'Tires'=>100,
'Window'=>1042,
'DoorHandle'=>917
);

/*Array of Associative Arrays*/
public $notifyPartners = array(
array(
'name' => 'Twitter',
'tag' => 'Social Network',
'url' => ''),
array(
'name' => 'Campaign Monitor',
'tag' => 'Email Marketing',
'url' => ''),
array(
'name' => 'Sendloop',
'tag' => 'Email Marketing',
'url' => ''),
array(
'name' => 'Highrise',
'tag' => 'CRM',
'url' => '')
);

/*Looping*/
foreach ($carParts as $key => $value) {
echo $key.'=>'.$value.'<br />';
}

while ($element = each($carParts)) {
echo $element['key'];
echo ' - ';
echo $element['value'];
echo '<br />';
}
 
 

Comments