A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » PHP » Associative Array Syntax Submit one!

Associative Array Syntax

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 />';
}

Subscribe to The Thread

  1. Nachzeher says:

    Thank’s for the tips!

    It was very useful to me!

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---