Tuesday 29 November 2011

Array Function In PHP


Ø       count()

  • counts the elements in an array, or properties in array

Syntax

                   int count(mixed $var [,int $mode)

Parameter
Description
$mode
If the optional mode parameter is set to COUNT_RECURSIVE or 1, count() will be recursively count the array.
This is particularly useful for counting all the elements of a multidimensional array.

Example

<?php
          $str  = array("php","c++","vb");
          echo count($str)."<br/>";
$s  = array("sem3"=>array("php","c++","vb") ,"sem4"=> array ("oracle","cg"));
          echo count($s,COUNT_RECURSIVE);
?>

Output  

3
7

Ø       list()

  • like array(), this is not really a function, but a language construct.
  • List() is used to assign a list of variables in one operation.

Syntax

                   void list(mixed $varname, mixed…….)

  • Note: list only works on numerical array and assume the array start at 0.

Example

<?php
          $arr = array("php","vb","c++");
          list($sub1,$sub2,$sub3)=$arr;
          echo $sub1.",".$sub2.",".$sub3;
?>

Output  

php,vb,c++

Ø       in_array()

  • This function will check whether the given value exists in the array or not. It will return either true or false value.

Syntax

                   bool in_array(variable to be search, array variable)

Example

<?php
          $arr = array("Mac","Irix","Linux","NT");
          if(in_array("Linux",$arr))
          {
                   echo "Got Linux";
          }
          else if(in_array("NT",$arr))
          {
                   echo "Got NT";
          }
?>

Output  

                   Got Linux

Ø       current()

  • Every array has an internal pointer to its current element, which is initialized to the first element inserted into the array.
  • The current() function simply returns the value of the array elements that’s currently being pointed to by the internal pointer. It does not move the pointer in any way.

Syntax

                   mixed current(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          next($arr);
          echo current($arr);
?>

Output  

                   Vb

Ø       next()

  • This will forward the position in the array to the next item it will return the mixed data type depending on the value in the array.

Syntax

                   mixed next(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          echo current($arr)."<br/>";
          echo next($arr);
?>

Output  

                   Php
                   vb

Ø       prev()

  • Return the array value in the previous place that’s pointed to by the internal array pointer, or false if there are no more elements.

Syntax

                   mixed prev(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          echo next($arr)."<br/>";
          echo prev($arr);
?>

Output  

                   vb
              php

Ø       end()

  • Returns the array value in the end of array that’s pointed to by the internal array pointer, or false if there are no elements.

Syntax

                   mixed end(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          echo end($arr);
?>

Output  

                   C++
             
Ø       each()

  • Returns the current key and value pair from an array and advance the array cursor. This pair is returned in a four – element array, with the keys 0, 1, key, and value. Elements 0 and key contain the key name of the array element, and 1 and value contain the data.

Syntax

                   Array   each(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          $str = each($arr);
          print_r($str);
?>

Output  

                   Array ( [1] => php [value] => php [0] => 0 [key] => 0 )

Ø       sort()

  • This function sorts an array. Elements will be arrange from lowest to highest when this function has completed.
  • Returns true on success or false on falure.

Syntax

                   bool sort(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          sort($arr);
          foreach($arr as $key=>$value)
          {
                   echo "Key $key & value $value<br/>";
          }
?>

Output  

                   Key 0 & value c++
                   Key 1 & value php
                   Key 2 & value vb

Note: This function assigns new keys for the elements in array it will remove any existing keys you may have assign, rather then just re-ordering the keys.

Ø       rsort()

  • This function sorts an array. Elements will be arrange from highest to lowest when this function has completed.
  • Returns true on success or false on falure.

Syntax

                   bool rsort(array $array)

Example

<?php
          $arr = array("php","vb","c++");
          rsort($arr);
          foreach($arr as $key=>$value)
          {
                   echo "Key $key & value $value<br/>";
          }
?>

Output  

                   Key 0 & value vb
                   Key 1 & value php
                   Key 2 & value c++

Note: This function assigns new keys for the elements in array it will remove any existing keys you may have assign, rather then just re-ordering the keys.

Ø       asort()

  • Sort an array and maintain index association. Elements will be arrange from lowest to highest when this function has completed.
  • This function sort an array such that array indices maintain their correlation with the array elements they are associated with.
  • This is used mainly when sorting associative arrays where the actual element order is significant.
  • Returns true on success and false on failure.

Syntax
                   bool asort(array $array)

Example

<?php
          $arr = array("a"=>"php","b"=>"vb","c"=>"c++");
          asort($arr);
          foreach($arr as $key=>$value)
          {
                   echo "$key=>$value<br/>";
          }
?>

Output  

                   c=>c++
                   a=>php
                   b=>vb

Ø       arsort()

  • Sort an array and maintain index association. Elements will be arrange from highest to lowest when this function has completed.
  • This function sort an array such that array indices maintain their correlation with the array elements they are associated with.
  • This is used mainly when sorting associative arrays where the actual element order is significant.
  • Returns true on success and false on failure.

Syntax

                   bool arsort(array $array)

Example

<?php
          $arr = array("a"=>"php","b"=>"vb","c"=>"c++");
          arsort($arr);
          foreach($arr as $key=>$value)
          {
                   echo "$key=>$value<br/>";
          }
?>

Output  

                   b=>vb
                   a=>php
                   c=>c++

Ø       array_merge()

  • array_ merge() Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
  • If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be append.
  • If only one array is given and the array is numerically indexed, the keys get reindexed  in a continuous way.

Syntax

                   array array_merge(array $array1 [,array $array2……])
         
Example

<?php
          $a = array("one","two","three");
          $b = array("1","2");
          print_r(array_merge($a,$b));
?>
Output  

                   Array ( [0] => one [1] => two [2] => three [3] => 1 [4] => 2 )

Ø       array_reverse()

  • Return an array with elements in reverse order.
  • array_reverse() takes input array and returns a new array with the order of the elements reversed, preserving the keys if preserve_keys is true.

Syntax

                   array array_reverse(array $array1 [,bool preserve_keys])

Example

<?php
          $a = array("php","vb","c++");
          print_r(array_reverse($a,true));
?>

Output  

                   Array ( [2] => c++ [1] => vb [0] => php )

Posted By : Hemangi Zala

No comments:

Post a Comment