Array Manipulation and Sum

Arrays manipulation  in php

Arrays are used to store set of elements in a variable.here is array manipulation functions are build in function.we are using that function to perform some operations in arrays.those operations are adding element in array,removing elements from array, storing element in array,sorting element in arrays, modify array element, reverse element from arrays.we cannot store more element in variable.it is possible only in arrays to store multiple data types values in arrays.wr can combine one array to another array to make all elements in single array.

Array manipulation functions are:

array_push()

array_pop()

array_shift()

array_unshift()

array_merge()

array_reverse()

sort()

rsort()

asort()

ksort()

array_slice()

array_splice()

array_search()

in_array()

array_unique()

array_keys()

array_values()

count()

array_push():

Normally push() is used in data structures.why push is using means we are adding elements in a stacks.like that array_push() function in php is used to add element in a array.

Syntax for array_push:

array_push($variablename,value);

Program for array_push() in php:

<?php

$var=array[14,24,34,44];

array_push($var,54);

print_r($var);

?>

Output:

array(

[0]=>14  [1]=>24 [2]=>34 [3]=>44 [4]=>54 )

array_pop():

array_pop() function in php is used for deleting last value from the array.it is like as ds pop function performs deletion operation,it only delete one last value from the array.

Syntax for array_pop():

array_pop($variablename,value);

Program:

<?php

$var=array[10,20,30,40,50];

array_pop($var);

print_r($var);

?>

Output :

array([0]=>10 [1]=>20 [2]=>30 [3]=>40 [4]=>50)

array_shift():

array_shift() function is used to delete the first element of an array.first element means array stored in index values zero.

Syntax for array_shift():

array_shift($varblename,value);

Program for array_shift():

<?php

$var=array[10,20,30,40,50];

array_shift($var);

print_r($var);

?>

Output :

array([0]=>20 [1]=>30 [2]=>40 [3]=>50)

array_unshift():

Array_unshift() function in php is used to add starting of an element in an array.here array_shift function deletes first element but unshift() function adds elements at starting of the array.

Syntax for array_unshift():

array_unshift($variable name,value);

Program for array_unshift():

<?php

$var=array[10,20,30,40,50];

array_unshift($var,60);

print_r($var);

?>

Output:

array([0]=>60 [1]=>10 [2]=>20 [3]=>30 [4]=>40 [5]=>60)

array_merge() in php:

array_merge() function in php is used to merge the two arrays data element.it is one of the main function in arrays.in php normally we are using concatenation for variables but in arrays we are using merge function.

Syntax for array_merge() in php:

array_merge($var1,$var2);

Program for array_merge():

?php

$var1 = [100, 200];

$var2 = [300, 400];

$var3= array_merge($var1, $var2);

print_r($var3);

?>

Output:

Array ( [0] => 100 [1] => 200  [2] => 300 [3] => 400)

array_reverse() in php:

array_reverse() function in php is used in arrays.if we want to change the order of array elements in reverse order,we are going to use this array element in reverse order.

syntax for array_reverse() in php:

array_reverse($variable name);

Program for array_reverse() in php:

<?php

$var1=[10,20,30,40,50];

$var2=array_reverse($var1);

print_r($var2);

?>

Output:

array([0]=>50 [1]=>40 [2]=>30 [3]=>20 [4]=> 10)

sort() in php:

sort() function in php is used for sorting element in array.for suppose we are storing five elements in array.i want that elements in ascending order by using sort() function.this is very useful function in php.

syntax for sort() in php:

sort($variable name);

Program for sort() in php:

<?php

$var1=[ 20,10,30,12,40];

$var2=sort($var1);

print_r($var2);

?>

Output:

array([0]=>10 [1]=>12 [2]=>20 [3]=>30 [4]=>40)

Leave a Comment