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

Comments

  1. User Avatar
    Nachzeher
    Permalink to comment#

    Thank’s for the tips!

    It was very useful to me!

  2. User Avatar
    Karl Groves
    Permalink to comment#

    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’] . “”;
    }

  3. User Avatar
    Dimitrios
    Permalink to comment#

    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 .””;
    }
    }

  4. User Avatar
    hem
    Permalink to comment#

    it would have been better had you shown the results of looping for the benefit of the newbies.

  5. User Avatar
    Ventura
    Permalink to comment#

    Great site, helped me a lot. I found very interesting tricks, Greetings.

  6. User Avatar
    dibiler
    Permalink to comment#

    Your last example:
    while ($element = each($carParts)) {
    echo $element[‘key’];
    echo ‘ – ‘;
    echo $element[‘value’];
    echo ”;
    }
    Can be put in a foreach better:
    foreach ($carParts as $element)
    {
    echo $element[‘name’];
    echo $element[‘tag’];
    echo $element[‘url’];
    }
    or if you don’t know the keys (Dimitrios example):
    foreach ($carParts as $element)
    {
    foreach ($element $k=>$v)
    echo $k.’:’.$v;
    }
    also, for performance, it’s a bit faster using a FOR loop, as Karl Groves wrote.

  7. User Avatar
    Attila Hajzer
    Permalink to comment#

    for the while loop couldn’t you do this too? :

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

    To reduce lines of code and in essence, avoid errors by accidentally mistyping echo or something?

  8. User Avatar
    Chintan Parikh
    Permalink to comment#

    Why call it “Array of Associative Arrays” instead of calling it multidimensional array?

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag