Arrays manipulation in php
PHP lo arrays anedi multiple values ni oka single variable lo store cheyadaniki chala useful. Normal variable lo usually oka value ni store chestam, kani array use cheste okesari chala values ni store cheyochu.
Array manipulation ante already unna array data tho different operations cheyadam. For example, array lo kotha elements add cheyadam, existing elements ni remove cheyadam, arrays ni merge cheyadam, elements ni reverse cheyadam, sorting cheyadam lanti operations ni array manipulation antam.
PHP lo ee operations ni easy ga perform cheyadaniki chala built-in array functions unnayi. Manam separate ga prati operation ki logic rayalsina avasaram lekunda, PHP provide chese functions ni use chesi arrays tho easy ga work cheyochu.
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.
Ee article lo array_push(), array_pop(), array_shift(), array_unshift(), array_merge(), array_reverse(), sort() mariyu rsort() lanti important array manipulation functions ni simple examples tho telusukundam.
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 = [14, 24, 34, 44];
array_push($var,54);
print_r($var);
?>
Output:
array(
[0]=>14 [1]=>24 [2]=>34 [3]=>44 [4]=>54 )
Example Program 2:
<?php $fruits = [“Apple”, “Banana”, “Mango”]; array_push($fruits, “Orange”, “Grapes”); print_r($fruits); ?>
Output:
Array
(
[0] => Apple
[1] => Banana
[2] => Mango
[3] => Orange
[4] => Grapes
)
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)
Example Program 2:
<?php $colors = [“Red”, “Green”, “Blue”, “Yellow”]; $lastColor = array_pop($colors); echo $lastColor; ?>
Output:
Yellow
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)
Example Program 2:
<?php $students = [“Rahul”, “Sita”, “Ravi”, “Anu”]; $firstStudent = array_shift($students); echo $firstStudent; ?>
Output:
Rahul
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($array, $value);
Example Program 1:
<?php $var = [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] => 50
)
Example Program 2:
<?php $fruits = [“Banana”, “Mango”]; array_unshift($fruits, “Apple”, “Orange”); print_r($fruits); ?>
Output:
Array
(
[0] => Apple
[1] => Orange
[2] => Banana
[3] => Mango
)
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():
array_merge($array1, $array2);
Example Program 1:
<?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
)
Example Program 2:
<?php $boys = [“Rahul”, “Ravi”]; $girls = [“Sita”, “Anu”]; $students = array_merge($boys, $girls); print_r($students); ?>
Output:
Array
(
[0] => Rahul
[1] => Ravi
[2] => Sita
[3] => Anu
)
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():
array_reverse($array);
Example Program 1:
<?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
)
Example Program 2:
<?php $days = [“Monday”, “Tuesday”, “Wednesday”, “Thursday”]; $reverseDays = array_reverse($days); print_r($reverseDays); ?>
Output:
Array
(
[0] => Thursday
[1] => Wednesday
[2] => Tuesday
[3] => Monday
)
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():
sort($array);
Example Program 1:
<?php $var1 = [20, 10, 30, 12, 40]; sort($var1); print_r($var1); ?>
Output:
Array
(
[0] => 10
[1] => 12
[2] => 20
[3] => 30
[4] => 40
)
Example Program 2:
<?php $prices = [500, 150, 300, 100, 250]; sort($prices); print_r($prices); ?>
Output:
Array
(
[0] => 100
[1] => 150
[2] => 250
[3] => 300
[4] => 500
)
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)
Conclusion
PHP lo array manipulation functions anedi arrays tho work chesetappudu chala useful. Elements ni add cheyadam, remove cheyadam, arrays ni merge cheyadam, reverse cheyadam mariyu sorting cheyadam lanti operations ni ee built-in functions tho easy ga perform cheyochu.
Ee functions meeda basic understanding unte PHP programs lo arrays ni manage cheyadam chala simple avutundi. Beginners munduga array_push(), array_pop(), array_shift(), array_unshift() lanti basic functions ni practice chesi, tarvata sorting mariyu searching functions ni nerchukunte concepts easy ga understand cheskovachu.