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

How to define PHP array


  <?php
$age = array(23, 21, 28, 30);
$age[4]=35;
print_r($age);
print "<hr>\n";
$age[1]=25;
print_r($age);
?>

How to get value of an element of php array


<?php
$age = array(23, 21, 28, 30);
$age[4]=35;
foreach($age as $item){
    
    print "$item
"; } ?>

How to automatically array of range of nubmer in php


<?php
$ageRange = range(18, 45);
foreach($ageRange as $item){
    
    print "$item
"; } ?>

Range with increment value


  <?php
$ageRange = range(18, 90, 5);
foreach($ageRange as $item){
    
    print "$item
"; } ?>

How to defind php array of string or text


 <?php
$country = array("Peru","Chad","Japan", "Canada", "Iran");
foreach($country as $item){
    
    print "$item
"; } ?>

How to get key of array in php


<?php
$country = array("Peru","Chad","Japan", "Canada", "Iran");
$country[6]="USA";
foreach($country as $key=>$item){
    
    print "$key = $item
"; } ?>

How to combine or merge arrays in php


<?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 , ";
    
}  
?>

How to revers a php array


 <?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 , ";
    
} 
?>

How to Sort php array


 <?php
$age = array(23, 5, 21, 45,9, 30);
foreach($age as $item){
    
    print "$item ";
    
}
print "<hr>";
sort($age);
foreach($age as $item){
    
    print "$item ";
    
} 
?>

Shuffle elements of php Array


<?php
$country = array("Peru","Chad","Japan", "Canada", "Iran", "Ukraine");

foreach($country as $item){
    
    print "$item ";
    
}
print "<hr>";
shuffle($country);
foreach($country as $item){
    
    print "$item ";
  ?> 

Print array element as HTML Table


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