<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ineedtutorials.com &#187; Associative Arrays</title>
	<atom:link href="http://www.ineedtutorials.com/tag/associative-arrays/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ineedtutorials.com</link>
	<description>Information made easy</description>
	<lastBuildDate>Thu, 14 May 2009 15:17:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Arrays in PHP &#8211; Tutorial</title>
		<link>http://www.ineedtutorials.com/code/php/arrays-in-php-tutorial</link>
		<comments>http://www.ineedtutorials.com/code/php/arrays-in-php-tutorial#comments</comments>
		<pubDate>Tue, 21 Oct 2008 19:39:15 +0000</pubDate>
		<dc:creator>Gertjan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[Associative Arrays]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[numeric arrays]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ineedtutorials.com/?p=122</guid>
		<description><![CDATA[What is an array? In computer science an array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type, in PHP however each element can be a different data type. Each element of an array has a unique identifying [...]]]></description>
			<content:encoded><![CDATA[<h3>What is an array?</h3>
<p>In computer science an array is a data structure  consisting of a group of elements that are accessed by indexing. In  most programming languages each element has the same data type, in PHP however each element can be a different data type. Each element of an array has a unique identifying index  number. Changes made to one element of an array do not affect the other  elements. </p>
<p> The  syntax of an array  is almost identical to the syntax of   other variables in PHP. It follows the same rules regarding variable  names, and  can be referenced in the same way as  any other variable. There are, however,  differences in how the  data within an array is accessed and how an array  is created.</p>
<p><span id="more-122"></span></p>
<h3>Arrays in PHP</h3>
<p>There are three different kind of arrays:</p>
<ul>
<li><strong>Numeric array</strong> &#8211; An array with a numeric  key</li>
<li><strong>Associative array</strong> &#8211; An array where each key is associated   	with a value</li>
<li><strong>Multidimensional array</strong> &#8211; An array containing one or more arrays</li>
</ul>
<p>PHP internally stores all arrays as associative arrays,  so the only difference between associative and indexed arrays is what  the keys happen to be. Some array features are provided mainly for use  with indexed arrays, because they assume that you have or want keys  that are consecutive integers beginning at 0. In both cases, the keys  are unique, you can&#8217;t have two elements with the same key,  regardless of whether the key is a string or an integer.</p>
<h3> Creating Numeric arrays</h3>
<p>We first start with creating an Indexed or Numerical array. In this example, we will create an indexed array consisting of some colors (our data elements). The position of first element will be zero by default.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12210"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p122code10"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$colors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Red&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Blue&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Green&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Yellow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> The array we have just created looks like this. Notice how the index starts at 0 by deafault.
</p>
<pre>
Array
(
    [0] => Red
    [1] => Blue
    [2] => Green
    [3] => Yellow
) 
</pre>
<p>You can also create this array in 2 other ways: </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code11'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12211"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p122code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Red&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blue&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Green&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Yellow&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// or</span>
&nbsp;
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Red&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blue&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Green&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Yellow&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Creating Associative arrays</h3>
<p>You have the possibility to use more meaningful keys.  Associative array means that you can assign an arbitray key to every  value. Associative arrays are sometimes referred to as dictionaries.  Our colors array can be defined as associative array like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code12'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12212"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p122code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$colors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;apple&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;sky&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;grass&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;green&quot;</span><span style="color: #339933;">,</span> 
				<span style="color: #0000ff;">&quot;banana&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Yellow&quot;</span>
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The array we have created now looks like this</p>
<pre>
Array
(
    [apple] => red
    [sky] => blue
    [grass] => green
    [banana] => Yellow
)
</pre>
<p>Again, there is a different way to create this array:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12213"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p122code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;apple&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;sky&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;grass&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;green&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;banana&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Yellow&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Creating Multidimensional arrays</h3>
<p> In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array.</p>
<p>In this example we create a multidimensional array, with automatically assigned ID keys:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12214"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p122code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$families</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                  <span style="color: #0000ff;">&quot;Simpson&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                     <span style="color: #0000ff;">&quot;Homer&quot;</span><span style="color: #339933;">,</span> 
                                     <span style="color: #0000ff;">&quot;Marge&quot;</span><span style="color: #339933;">,</span> 
                                     <span style="color: #0000ff;">&quot;Bart&quot;</span><span style="color: #339933;">,</span>
                                     <span style="color: #0000ff;">&quot;Lisa&quot;</span><span style="color: #339933;">,</span>
                                     <span style="color: #0000ff;">&quot;Maggie&quot;</span>
                                    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>			   
                  <span style="color: #0000ff;">&quot;Szyslak&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Moe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                  <span style="color: #0000ff;">&quot;Flanders&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                   <span style="color: #0000ff;">&quot;Ned&quot;</span><span style="color: #339933;">,</span>
                                   <span style="color: #0000ff;">&quot;Maude&quot;</span><span style="color: #339933;">,</span> 
                                   <span style="color: #0000ff;">&quot;Todd&quot;</span><span style="color: #339933;">,</span>
                                   <span style="color: #0000ff;">&quot;Rod&quot;</span>
								   <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> The array above would look like this:</p>
<pre>
  Array
(
    [Simpson] => Array
        (
            [0] => Homer
            [1] => Marge
            [2] => Bart
            [3] => Lisa
            [4] => Maggie
        )

    [Szyslak] => Array
        (
            [0] => Moe
        )

    [Flanders] => Array
        (
            [0] => Ned
            [1] => Maude
            [2] => Todd
            [3] => Rod
        )

)
  </pre>
<p>Again, there are different ways to create this array:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12215"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p122code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Homer&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Marge&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bart&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lisa&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Maggie&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Szyslak&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Moe&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ned&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Maude&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Todd&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Rod&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// or</span>
&nbsp;
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Homer&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Marge&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bart&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lisa&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Simpson&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Maggie&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Szyslak&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Moe&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ned&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Maude&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Todd&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$families</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Flanders&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Rod&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Accessing the array elements </h3>
<p>You access the contents using the array name and a key, so you can access the information stored in the families array as $families["Flanders"][3], $families["Simpson"][2], and $families["Szyslak"][0].</p>
<h4>Using loops</h4>
<p>To access the data stored in the array, one can manualy echo each piece of the array. This is not verry effective, when the array grow, so does the amount of effort one has to put into outputting the data. This is why you use a looping construct to do this work for you.</p>
<h5>The for loop</h5>
<p> The for loop has three statements. </p>
<ul>
<li>In the first statement, we initialize a counter variable.</li>
<li>In the second statement, we set a condition (a max/min number value) until the counter is reached.</li>
<li>In the third statement, we set a value by how much we want the counter variable to incremented by.</li>
</ul>
<p>In this example the for loop, itterates through the array $colors and prints each item on the screen.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12216"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p122code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$colors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Red&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Blue&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Green&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Yellow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Output:</strong></p>
<pre>
Red
Blue
Green
Yellow
</pre>
<h5>Using the foreach loop</h5>
<p>Foreach loop is most often used to print elements in a array.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12217"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p122code17"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$colors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Red&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Blue&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Green&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Yellow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$colors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$color</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Output:</strong></p>
<pre>
Red
Blue
Green
Yellow
</pre>
<h5>Using the foreach loop with associative arrays</h5>
<p>When using associative arrays the indices in an array are not numbers, you cannot use a simple  counter in a for loop to work with the array. However, you can use the  foreach loop.</p>
<p>The foreach loop has a slightly different structure when using  associative arrays. You can use it exactly as  in the previous example,  or you can incorporate the keys as well:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p122code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12218"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p122code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
&nbsp;
<span style="color: #000088;">$colors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;apple&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;sky&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;grass&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;green&quot;</span><span style="color: #339933;">,</span> 
				<span style="color: #0000ff;">&quot;banana&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Yellow&quot;</span>
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$colors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$fruit</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$fruit</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">' is '</span><span style="color: #339933;">.</span><span style="color: #000088;">$color</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Output:</p>
<pre>apple is red
sky is blue
grass is green
banana is Yellow</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ineedtutorials.com/code/php/arrays-in-php-tutorial/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
