Sunday, February 4, 2018

PHP ARRAYS

                                       PHP  ARRAYS


An array is a collection of similar data types.Arrays can store one or more values in a single variable name.

Instead of having many similar variables, we can store the data as elements in an array.Each element in the array has its own ID so that it can be easily accessed.

There are three different kinds of arrays:

               1.Numeric array
       
               2.Associative array

               3.Multidimensional array


Numeric array: 

                         A numeric array stores each element with a numeric identification key element.

Example 1:

        In this example, the ID key is automatically assigned

  $names  = array("web","designing", "company");

Example 2:

    In this example we assign the ID key manually

  $names[0]   ="web";
  $names[1]   ="designing";
 $names[2]   ="company";

The ID keys can be used in a script:

        <?php
        $names[0] ="web";
        $names[1] ="designing";
        $names[2] ="company";
        echo $names[0] . $names[1] . " " . $names [2];
        ?>

Output: 
   web designing  company

Associative arrays:

   In an associative array, each ID key is associated with a value, with the help of associative array we can use the values as keys and assign values to them.

In this below example we use an array to assign ages to a different person. 

Example 1: 

$ages = array("owen" =>25, "morris"=>26, "ponting"=>27);

Example 2:

In this example it shows a different way of creating the array; 

$ages['owen']  ="25";

$ages['morris']  ="26";

$ages['ponting']  ="27";

we will see the ID scripts in the PHP format:

 <?php

$ages[ 'owen'] = "25";

$ages[ 'morris'] = "26";

$ages[ 'ponting'] = "25";

echo "owen is ".$ages['owen'] . "years old.";

?>

Output:

owen is 25 years old.

Multidimensional arrays:

 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.
    
Syntax:

   $fruits = array
  (
  array("banana",10,20),
  array("apple",20,25),
  array("orange",15,30),
  array("pineappleneapple",23,20)
  );

Example:

<html>
<body>

<?php
$fruits = array
  (
  array("banana",10,20),
  array("apple",20,25),
  array("orange",15,30),
  array("pineapple",23,20)
  );

echo $fruits[0][0].": In stock: ".$fruits[0][1].", sold: ".$fruits[0][2].".<br>";
echo $fruits[1][0].": In stock: ".$fruits[1][1].", sold: ".$fruits[1][2].".<br>";
echo $fruits[2][0].": In stock: ".$fruits[2][1].", sold: ".$fruits[2][2].".<br>";
echo $fruits[3][0].": In stock: ".$fruits[3][1].", sold: ".$fruits[3][2].".<br>";
?>

</body>

</html>

Output:

banana: In stock: 10, sold: 20.
apple: In stock: 20, sold: 25.
orange:In stock:15, sold:30.
pineapple:In stock:23, sold: 20.



No comments:

Post a Comment

A R Murugadoss --- Film Career and Biography

         A R Murugadoss --- Film Career a Biography  Early Life :          A.R Murugadoss was Born on 25th September in the Year o...