Sunday, January 21, 2018

PHP Array function for BCA,B.Sc(IT),PGDCA,MCA,M.Sc(IT) and all IT Students


--------------------------------------------------------------------------------------------------------------------------
  Prepared By   : Uday Shah (HOD-IT)
  Contact No     : 7600044051
  E Mail            : rupareleducation@gmail.com


PHP Array Function

No
Name
Description
1
List
It assign variable as if they were an array.
Syntax:
list(varname)
Example:
<?php
$info=array(‘coffee’,’brown’,’caffine’);
list($drink,$color,$power)=info;
echo “$drink is $color and $power makes it special.”;
?>
2
count()
It counts element an array or properties in an object. It returns the number of elements in a variable.
Syntax
int count(element)
Example
$a[0]=1;
$a[1]=3;
$a[2]=5;
$result=count($a);
3
in_array()
Checks if a value is exists in an array or not.
Syntax: in_array(elements)
<?php
     $x=array(“NT”,”UNIX”,”Linux”,”Solaries”);
     if(in_array(“Linux”,$x))
     {
              Echo “Got Linux”;
     }
     else
     {
               Echo “Not Found”;
     }
?>
4
current()
It returns the current elements in an array.
Syntax:
current (list of element)
Example.
<?php
$transport=array(“foot”,”bike”,”car”,”Plane”);
$mode=current($transport);
?>
5
next()
It is used to return next element in an array. That is pointed to the internal array pointer.
Syntax: next (list of element)
Example
<?php
$transport=array(“foot”,”bike”,”car”,”Plane”);
$mode=current($transport);         //ans=foot
$mode=next($transport);   //ans=bike
?>
6
prev()
It returns the array value in the previous place that is pointed to the internal array pointer.
Syntax:
prev (list of element)
Example
<?php
$transport=array(“foot”,”bike”,”car”,”Plane”);
$mode=current($transport);
$mode=next($transport);
$mode=prev($transport);
?>
7
end()
It is used to set internal pointer of an array to its last element.
Syntax:
end (list of element)
Example.
<?php
$transport=array(“foot”,”bike”,”car”,”Plane”);
$mode=current($transport);         //ans=foot
$mode=next($transport);   //ans=bike
$mode=next($transport);   //ans=car
$mode=prev($transport);   //ans=bike
$mode=end($transport);     //ans=plane
?>
8
sort()
It sorts an array element be arranged from lowest to highest when this function has completed.
This function assigns new key for the element in array, it will remove any existing keys you may have assigned.
Syntax: sort(list of array element)
Ex:
<?php
        $fruits=array(“lemon”,”orange”,”banana”,”apple”);
        Sort($fruits);
        Foreach($fruits as $key=>$val)
       {
Echo “fruits[“. $key .”]=”.$val;
        }
?>
O/P
Fruits[0]=apple
Fruits[1]=banana
Fruits[2]=lemon
Fruits[3]=orange
9
rsort()
This function sorts an array in reverse order from highest to lowest value.
Syntax
rsort(list of element)
Example
<?php
       $fruits=array(“lemon”,”orange”,”banana”,”apple”);
        rsort($fruits);
        Foreach($fruits as $key=>$val)
        {
Echo “fruits[“. $key .”]=”.$val;
         }
?>
10
asort()
It sorts an array and maintain index association this function sorts an array such that array indices maintain their co relation with the array element they are associated with.
Syntax
asort(list of element)
Example
<?php
$fruits=array(“d”=>”lemon”,”a”=>”orange”,”b”=>”banana”,”c”=>”apple”);
asort($fruits);
foreach($fruits as $key =>$val)
{
          Echo “$key=$val”;
}
?>
O/P
c=apple
b=banana
d=lemon
a=orange
11
arsort()
Sort an array in reverse order and maintain index association.
Syntax:
arsort(list of element)
Example
<?php
$fruits=array(“d”=>”lemon”,”a”=>”orange”,”b”=>”banana”,”c”=>”apple”);
arsort($fruits);
foreach($fruits as $key =>$val)
{
              Echo “$key=$val”;
}
?>
12
array_shift()
array_shift () function removes the first element from an array, and returns the value of the removed element.

Syntax

Array_shift(array)

Example

Remove the first element (red) from an array, and return the value of the removed element:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);
?>
13
Array_diff ()
The array_diff () function compares the values of two (or more) arrays, and returns the differences.This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

Syntax

Array_diff(array1,array2,array3...);

Example

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);
?>
14
Array_merge_recursive ()
The array_merge_recursive () function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive () function makes the value as an array.

Syntax

Array_merge_recursive(array1,array2,array3...)
Example
Merge two arrays into one array:
<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));
?>
15
Array_slice ()
The array_slice() function returns selected parts of an array.

Syntax

Array_slice(array,start,length,preserve)

Example

Start the slice  the 2 array element, and return the rest of the elements in the array:
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
16
Array_unique ()
The array_unique () function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.

Syntax

array_unique(array)

Example

Remove duplicate values from an array:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
17
Array_unshift ()
The array_unshift() function inserts new elements to an array. The new array values will be inserted in the beginning of the array.

Syntax

array_unshift(array,value1,value2,value3...)
Example
Insert the element "blue" to an array:
<?php
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a);
?>
18
Array_keys ()
The array_keys() function returns an array containing the keys.

Syntax

Array_keys(array,value,strict)

Example

Return an array containing the keys:
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Innova");
print_r(array_keys($a));
?>
19
Array_key_exists()

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

Syntax

array_key_exists(key,array)
Example
Check if the key "Volvo" exists in an array:
<?php
        $a=array("Volvo"=>"XC90","BMW"=>"X5");
        if (array_key_exists("Volvo",$a))
        {
              echo "Key exists!";
        }
        else
        {
              echo "Key does not exist!";
       }
?>
20
Array_push ()
The array_push () function inserts one or more elements to the end of an array.

Syntax

array_push(array,value1,value2...)
Example
Insert "blue" and "yellow" to the end of an array:
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
21
array_pop()
The array_pop() function deletes the last element of an array.

Syntax

array_pop(array)
Example
Delete the last element of an array:
<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>
22
Array_multisort()

The array_multisort () function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

Syntax

Array_multisort(array1,sorting order,sorting type,array2,array3...)
Example
Return a sorted array in ascending order:
<?php
$a=array("Dog","Cat","Horse","Bear","Zebra");
array_multisort($a);
print_r($a);
?>
23
array_search()
The array_search() function search an array for a value and returns the key.

Syntax

array_search(value,array,strict)

Example

Search an array for the value "red" and return its key:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>
24
array_merge()
This function is used to merge one or more arrays.
Syntax
array array_merge(array1,array2,array……)
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.
Example
<?php
          $array1=array(“red”,”green”,”blue”);
          $array2=array(“pink”,”brown”,”yellow”);
          $result=array_merge($array1,$array2);
          print_r($result);
?>
25
Array_reverse ()
It returns an array with elements in reverse order.
Syntax
array array_reverse(array values);
Array_reverse() takes input an array and returns a new array with the order of the elements reversed.
Example
<?php
       $input=array(“php”,4.0,”red”);
       print_r(array_reverse($input));
?>
26
each()
It returns the current key and value pair from an array.
Syntax:
each(array);
Example:
<?php
       $foo=array(“red”,”green”,”blue”);
       $bar=each($foo);
       Print_r($bar)
?>




Please share and comment it... Thank You.

:: Best of Luck :: 

https://myshopprime.com/uday.shah3/shop