Code Snippet
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 />';
}
Thank’s for the tips!
It was very useful to me!
Using your looping example won’t work on a multidimensional array (you call it an array of arrays).
Example would be:
$count = count( $notifyPartners );
for($x = 0; $x < $count; $x++){
echo $notifyPartners[$x]['name'] . "”;
echo $notifyPartners[$x]['tag'] . “”;
echo $notifyPartners[$x]['url'] . “”;
}
Another example (portable) .
$a = 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’ => ”));
foreach ($a as $loop1) {
foreach ($loop1 as $key => $val) {
echo $key .” : “. $val .”";
}
}
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.