Monday, 20 May 2013

array in php


PHP Array:

    An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length. 

    In PHP you can give a single definition to a variable. If you wanted to add a list of items to a variable, you would be creating an array. Below is how you create an array in PHP.
    Example
    <?php $city = array("jaipur","agra","aligarh"); print_r($city); ?>


    Output
         Array ( [0] => jaipur [1] => agra [2] => aligarh ) 

    Note- In our example above, we can then print the contents of the array using print_r().
    An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

    Types Of Array in php

    There are three different kind of arrays in php and each array value is accessed using an ID which is called array index.
  • Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion
  • 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.
  • Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices
  • PHP Numeric Array:

      Numeric arrays is used to store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.
      Here we have used array() function to create array. This function is explained in function reference. 

      A simple example of numeric array is describe below :
      Example : First method to create array
      <?php $city = array("jaipur","aligarh","agra"); foreach( $city as $value ) { echo "Value is $value &lt;br /&gt;"; } ?>


      Output
           Value is jaipur 
      Value is aligarh 
      Value is agra 


      Second method to create array
      <?php $name[0] = "rex"; $name[1] = "raj singh"; $name[2] = "www"; $name[3] = "maddy"; foreach( $name as $value ) { echo "Value is $value &lt;br /&gt;"; } ?>


      Output
      Value is rex 
      Value is raj singh 
      Value is www 
      Value is maddy 

    PHP Associative Array:

      The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values. 

      In associative array we declare index of array according to our business requirement or their individual value.
      Syntax
      <?php array( key1 =&gt; value, key2 =&gt; value2, key3 =&gt; value3, ... ) ?>


      A simple example of associative array declaration is given below :
      <?php $city = array( "jaipur" =&gt; "jaipur is called pink city", "aligarh" =&gt; "aligarh is my local city", ); // as of PHP 5.4 $array = [ "jaipur" =&gt; "jaipur is called pink city", "aligarh" =&gt; "aligarh is my local city", ]; ?>




      Second method to create array
      <?php $city = array( "rex" =&gt; "jaipur", "www" =&gt; "faizabad", "raj" =&gt; "aligarh" ); echo "city of rex-".$city['rex']."&lt;br /&gt;"; echo "city of www-".$city['www']."&lt;br /&gt;"; echo "city of raj-".$city['raj']."&lt;br /&gt;"; /* Second method to create array. */ $city['max'] = "agra"; $salaries['adil'] = "delhi"; $salaries['zara'] = "ajmer"; echo "city of max-".$city['rex']."&lt;br /&gt;"; echo "city of adil-".$city['www']."&lt;br /&gt;"; echo "city of zara-".$city['raj']."&lt;br /&gt;"; ?>


      Output
      city of rex-jaipur
      city of www-faizabad
      city of raj-aligarh
      city of max-jaipur
      city of adil-faizabad
      city of zara-aligarh

    PHP Multi Dimensional Array:

      A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
      Example
      In this example we create a two dimensional array to store id and password of three students :
      This example is an associative array, you can create numeric array in the same fashion.
      <?php $data = array( "rex" =&gt; array ( "id" =&gt; "rex@gmail.com", "password" =&gt; "111" ), "www" =&gt; array ( "id" =&gt; "www@gmail.com", "password" =&gt; "333" ), "raj" =&gt; array ( "id" =&gt; "thakur.raj108@gmail.com", "password" =&gt; "111" ), ); /* Accessing array values */ echo $data['rex']['id']."&lt;br /&gt;"; echo $data['rex']['password']."&lt;br /&gt;"; echo $data['www']['id']."&lt;br /&gt;"; echo $data['www']['password']."&lt;br /&gt;"; echo $data['raj']['id']."&lt;br /&gt;"; echo $data['raj']['password']."&lt;br /&gt;"; ?>


      Output
      rex@gmail.com
      111
      www@gmail.com
      333
      thakur.raj108@gmail.com
      111

No comments:

Post a Comment