Array manipulation functions in php-part2
Some functions are discussed already.now in this part 2 series we can see remaining array functionsnin php.
1.rsort()
2.asort()
3.ksort()
4.array_slice()
5.array_splice()
6.array_search()
7.in_array()
8.array_unique()
9.array_keys()
10.array_values()
11.count()
rsort():
rsort() function in php is very useful.that function is used for sorting elements in descending order.element values are stored in indexed in arrays.lets see some
example program :
<?php
$arr=array[90,10,30,40];
rsort($arr);
print_r($arr);
?>
Output:
array(
[0]=>90 [1]=>40 [2]=>30 [3]=>10)
asort():
asort() function is used to print elements in ascending order.these asort() is used only for associate array.associate array means storing values pairs.
Program for asort() in php:
<?php
$arr=[“a”=20,”b”=60,”c”=50];
asort($arr);
print_r($arr);
?>
Output:
array( [a]=20 [c]=50 [b]=60)
ksort():
Ksort() function is used to give ascending order of an elements .these uses associate arrays and sorting is possible with keys not with values.
Example program for ksort():
$arr=[“b”=20,”a”=60,”d”=50];
asort($arr);
print_r($arr);
?>
Output:
array( [a]=60 [b]=20 [d]=50)
array_slice():
array_slice() function is important in php.this array_slice function is used to slice a program with out any modification of a program.we can get some output means part of elements in an array.
Syntax for array_slice():
array_slice($array, $start, $length);
$start=start means starting index
$length=length of elements to print
Program for array_slice():
<?php
$a = [20, 40, 88, 60, 26];
$res = array_slice($a, 1, 4);
print_r($res);
?>
Output:
Array
(
[0] => 40
[1] => 88
[2] => 40
[3]=>60
)
array_search():
array_search() function is used to check whether given value is present in array or not.if value is present it gives true .if values is not found it gives false .this array_search() function is used for searching elements in an array.we should know difference between key and value.key means values stored in that.value means that particular value.it also used to know the index values stored that element.
Syntax for array_search():
array_search($value, $array);
Program for array_search():
<?php
$fruits = [“Apple”, “Banana”, “Mango”,”strawberry”];
echo array_search(“Banana”, $fruits);
?>
Output:
1
Explanation:
4 strings values are stored in fruits variable.here we are using array_search() for banana.banana is stored in index 1.so our output is displayed as 1.
in_array():
in_array() is similar to array_search() function.but the difference is array_serach() gives index values if elements is exist.but in in_array() it gives true if element exist nor it gives false element not found.we should check this condition by using if condition.
Syntax for in_array():
in_array($value, $array);
Program for in_array():
<?php
$color= [“Red”, “Green”, “Blue”,”yellow”];
if(in_array(“Blue”, $color))
{
echo “Value Found”;
}
else
{
echo “Value Not Found”;
}
?>
Output:
Value Found
array_unique():
array_unique() function is used to delete duplicate elements in an array.duplicate means if same element their in two times it removes one element in an array.it gives remaining unique elements in the array
Syntax for array_unique():
array_unique($array);
Program for array_unique():
<?php
$a = [10, 20, 10, 30, 20, 30,60,40];
$result = array_unique($a);
print_r($result);
?>
Output:
Array
(
[0] => 10
[1] => 20
[3] => 30
[6] => 60
[7]=>40
)
Explanation:
In the above program we are taking eight elements in an array.all array element stored in index.index value start with 0.here array_unique() function is used to remove duplicate elements and prints remaining unique elements with same index number.
array_keys():
array_keys() function in php is used to gives only keys.this function is used in associate arrays .in associate arrays we are storing element in the form of key -value pair.
Syntax for array_keys():
array_keys($array);
Program for array_keys():
<?php
$det = [
“Name” => “chinna”,
“Age” => 25,
“City” => “ongole”,
“group”=>”bsc”
];
print_r(array_keys($det));
?>
Output:
array(
[0]=>Name
[1]=>Age
[2]=>City
[3]=>group)
array_values():
array_values() function in php is used to give only values in the array.this function works in only associate array type.it gives values stored in which indexes.
Syntax for array_values():
array_values($array);
Program for array_values():
<?php
$det = [
“Name” => “chinna”,
“Age” => 25,
“City” => “ongole”,
“group”=>”bsc”
];
print_r(array_valyes($det));
?>
Output:
array(
[0]=>chinna
[1]=25
[2]=ongole
[3]=>bsc )
count():
count() function in php is used to count the number of elements in an array.it counts and gives output an numeric values.
Syntax for count ():
count($array);
Program for count() in php:
$a = [10, 20, 30, 40, 50,70];
echo count($a);
?>
Output:
6
1 thought on “php-array-manipulation-functions-part-2”