In PHP Array function allows us to access and manipulate arrays
MSBTE 4 Mark:Question 1: Explain any four Array Functions in PHP
MSBTE 6 Marks:
Question 1: Explain Array functions
array_diff():
Compare the values of two arrays and return the difference.
Syntax: array_diff(array1,array2,...)
array_intersect():
return matched values from an arrays
Syntax: array_intersect(array1,array2,...)
array_chunk():
Split am array into blocks with preserve keys.
Syntax: array_chunk(array,size,preserve_key)
array_combine():
create an array by using the elements from one "keys" array and one "values" array.
syntax: array_combine(keys,values)
array_unique():
Remove duplicate values from an array.
syntax: array_unique(array,sorttype)
array_count_values():
Count all values of an array.
array_merge():
Merge two arrays into one array.
syntax: array_merge(array1,array2,...)
array_pop():
remove the last element of an array and return the deleted element.
syntax: array_pop(array)
array_product()
Calculate the product and return the product of an array.
syntax: array_product(array)
array_push()
Insert one or more elements into the end of an array.
syntax: array_push(array,value1,value2,...)
array_reverse()
Return an array in the reverse order.
syntax: array_reverse(array,preserve)
array_search()
Searches an array for a value and return the key.
syntax:array_search(value,array)
array_sum()
Return the sum of all elements(values) in the array.
syntax:array_sum(array)
count()
Return the number of elements in an array.
syntax: count(array)
Example:
<?php
$a1=array('C','c++','Java','Python');
$a2=array('C','c++','Java','.Net','PHP');
echo "<br><br>Diff:";
print_r(array_diff($a1,$a2));
echo "<br><br>Intersect:";
print_r(array_intersect($a1, $a2));
echo "<br><br>Chunk:";
print_r(array_chunk($a1,2,true));
echo "<br><br>combine:";
$k=array('1','2','3','4');
print_r(array_combine($k,$a1));
$k1=array('1','2','3','4','1','3');
echo "<br><br>Unique:";
print_r(array_unique($k1));
echo "<br><br>Count values:";
print_r(array_count_values($k1));
echo "<br><br>merge:";
print_r(array_merge($a1,$a2));
echo "<br><br>POP:";
print_r(array_pop($k1));
echo "<br><br>product:";
print_r(array_product($k));
echo "<br><br>Push:";
print_r(array_push($k,'5'));
print_r($k);
echo "<br><br>Reverse:";
print_r(array_reverse($k));
echo "<br><br>Search:";
print_r(array_search('Java', $a1));
echo "<br><br>Sum:";
print_r(array_sum($k1));
echo "<br><br>Count:";
print_r(count($a1))
?>
Output:
Diff:Array ( [3] => Python )
Intersect:Array ( [0] => C [1] => c++ [2] => Java )
Chunk:Array ( [0] => Array ( [0] => C [1] => c++ ) [1] => Array ( [2] => Java [3] => Python ) )
combine:Array ( [1] => C [2] => c++ [3] => Java [4] => Python )
Unique:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Count values:Array ( [1] => 2 [2] => 1 [3] => 2 [4] => 1 )
merge:Array ( [0] => C [1] => c++ [2] => Java [3] => Python [4] => C [5] => c++ [6] => Java [7] => .Net [8] => PHP )
POP:3
product:24
Push:5Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Reverse:Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )
Search:2
Sum:11
Count:4
Comments
Post a Comment
If you have any query, please let us know