arrays in php

In this example, arrays are declared in PHP using name/value pairs and without name value pairs

<html>
<head>
</head>
<body>
<?php
$array = array(
              "KHI" => "Karachi",
        "LHR" => "Lahore",
        "ISL" => "Islamabad");

$array1 = array("pakistan", "usa", "england");			  
        
        
//var_dump($array);
//print_r($array);

foreach($array as $key => $value)
{
  echo $key . " " . $value . "<br>";
}

$i=0;
for($i=0; $i<sizeof($array1); $i++)
{
  echo $array1[$i] . "<br>";
}


?>    
</body>
</html>

Another example

<html>
<head>
</head>
<body>
<?php
$myarray = array("KHI"=>"karachi", "LHR"=>"lahore", "ISLD"=>"islamabad");

foreach($myarray as $key=>$value)
{
  echo $value ."<br>";
}

unset($myarray['ISLD']);
echo "<br>";
foreach($myarray as $value)
{
  echo $value ."<br>";
}

?>    
</body>
</html>

More examples

<html>
<head>
</head>
<body>
<?php
$myarray = array("fruits" => array( "a" => "orange",
                   "b" => "banana",
                   "c" => "apple"
                  ),
        "numbers" => array(1,2,3,4,5),
        "positions" => array (
                              "first",
                    5=>"second",
                    "third"
                    )
        );				   
foreach($myarray as $key => $value)
{
  foreach($value as $key1 => $value1)
  {
    echo $key1 . " " . $value1 . "<br>";
  }
}

?>    
</body>
</html>

 

Add a Comment

Your email address will not be published. Required fields are marked *