PHP Array for Absolute Beginners Tutorial video with source code startPHP-006


		
<h2>How to define PHP array</h2>
		
<pre><code class="php">
  &lt;?php
$age = array(23, 21, 28, 30);
$age[4]=35;
print_r($age);
print "&lt;hr&gt;\n";
$age[1]=25;
print_r($age);
?&gt;
</code></pre>          
		
   
   <h2>How to get value of an element of php array</h2>
		
<pre><code class="php">
&lt;?php
$age = array(23, 21, 28, 30);
$age[4]=35;
foreach($age as $item){
    
    print "$item<br/>";
    
}
?&gt;
</code></pre>          
		
<h2>How to automatically array of range of nubmer in php</h2>
		
<pre><code class="php">
&lt;?php
$ageRange = range(18, 45);
foreach($ageRange as $item){
    
    print "$item<br/>";
    
} 
?&gt; 
</code></pre>          
		
		<h2>Range with increment value</h2>
		
<pre><code class="php">
  &lt;?php
$ageRange = range(18, 90, 5);
foreach($ageRange as $item){
    
    print "$item<br/>";
    
}
?&gt;
</code></pre>    

      		<h2>How to defind php array of string or text</h2>
		
<pre><code class="php">
 &lt;?php
$country = array("Peru","Chad","Japan", "Canada", "Iran");
foreach($country as $item){
    
    print "$item<br/>";
    
} 
?&gt;
</code></pre>  

		<h2>How to get key of array in php</h2>
		
<pre><code class="php">
&lt;?php
$country = array("Peru","Chad","Japan", "Canada", "Iran");
$country[6]="USA";
foreach($country as $key=>$item){
    
    print "$key = $item<br/>";
    
}  
?&gt;
</code></pre>  

		<h2>How to combine or merge arrays in php</h2>
		
<pre><code class="php">
&lt;?php
$country = array("Peru","Chad","Japan", "Canada", "Iran");
$fruits = array("banana", "apple", "grapes");

$nAr = array_merge($country,$fruits );
foreach($nAr as $key=>$item){
    
    print "$key = $item , ";
    
}  
?&gt;
</code></pre>  

		<h2>How to revers a php array</h2>
		
<pre><code class="php">
 &lt;?php
$country = array("Peru","Chad","Japan", "Canada", "Iran");
$fruits = array("banana", "apple", "grapes");

$reversed = array_reverse($country);
foreach($reversed as $key=>$item){
    
    print "$key = $item , ";
    
} 
?&gt;
</code></pre>  

<h2>How to Sort php array</h2>
<pre><code class="php">
 &lt;?php
$age = array(23, 5, 21, 45,9, 30);
foreach($age as $item){
    
    print "$item ";
    
}
print "&lt;hr&gt;";
sort($age);
foreach($age as $item){
    
    print "$item ";
    
} 
?&gt;
</code></pre>  

<h2>Shuffle elements of php Array</h2>
<pre><code class="php">
&lt;?php
$country = array("Peru","Chad","Japan", "Canada", "Iran", "Ukraine");

foreach($country as $item){
    
    print "$item ";
    
}
print "&lt;hr&gt;";
shuffle($country);
foreach($country as $item){
    
    print "$item ";
  ?&gt; 
</code></pre>  


<h2>Print array element as HTML Table </h2>
<pre><code class="php">
&lt;?php
$country = array("Peru","Chad","Japan", "Canada", "Iran", "Ukraine");
$pop = array( 5.3, 18, 16, 35, 25, 82);
?&gt;
&lt;table style="width:100%"&gt;
&lt;?php
foreach($country as $key=&gt;$item){
?&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;?php echo $item?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $pop[$key]?&gt;&lt;/td&gt; 
  &lt;/tr&gt;
&lt;?php
    
}
?&gt;
 &lt;/table&gt;
	  
</code></pre>