Wednesday, 5 February 2014

Php Array

Array :- A data structure that stores one or more similar type of values in a single value.

Use array() function to create array

Example:-

Here you can find the code please click below
DEMO

Three Types Of Array :-
  • Numeric array - An array with a numeric index. These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero(0).
<?php

/* 1st method to create array. */

$num = array( 1, 2, 3, 4, 5,6,7,8,9);
foreach( $num as $value )
{
  echo "Value is $value <br />";
}
/* 2nd method to create array. */
$num[0] = "one";
$num[1] = "two";
$num[2] = "three";
$num[3] = "four";
$num[4] = "five";
$num[5] = "six";
$num[6] = "seven";
$num[7] = "eight";
$num[8] = "nine";

foreach( $num as $value )
{
  echo "Value is $value <br />";
}
?>
  • Associative array - An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.Associative array is very similar to numeric arrays in term of functionality but they are different in terms of their index.
Note:- Don't keep this array inside the double quote while printing it otherwise it will not give any return value.


<?php

/* 1st method to associate create array. */
$salaries = array("ritu" => 25000,"raj" => 20000,"kumar" => 15000);

echo "Salary of ritu is ". $salaries['ritu'] . "<br />";
echo "Salary of raj is ".  $salaries['raj']. "<br />";
echo "Salary of kumar is ".  $salaries['kumar']. "<br />";

/* 2nd method to create array. */
$salaries['ritu'] = "high";
$salaries['raj'] = "medium";
$salaries['kumar'] = "low";

echo "Salary of ritu is ". $salaries['ritu'] . "<br />";
echo "Salary of raj is ".  $salaries['raj']. "<br />";
echo "Salary of kumar is ".  $salaries['kumar']. "<br />";
?>

  • Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices.Each element in the main array can also be an array,And each element in the sub-array can be an array.

<?php
   $marks = array("ritu" => array("physics" => 35,"maths" => 30,"chemistry" => 39),
"raj" => array
                (
                "physics" => 30,
                "maths" => 32,
                "chemistry" => 29
                ),
                "kumar" => array
                (
                "physics" => 31,
                "maths" => 22,
                "chemistry" => 39
                )
    );
   /* Accessing multi-dimensional array values */
   echo "Marks for ritu in physics : " ;
   echo $marks['ritu']['physics'] . "<br />"; 
   echo "Marks for raj in maths : ";
   echo $marks['raj']['maths'] . "<br />"; 
   echo "Marks for kumar in chemistry : " ;
   echo $marks['kumar']['chemistry'] . "<br />"; 
?>





No comments:

Post a Comment

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...