Friday, July 31, 2015

Array Function in PHP

Array functions allow us to interact with and manipulate array in various ways.
Arrays are essential for storing, managing and operating a set of variable.

There are various types of array functions are available. Here we will see some important array functions with examples.
1. array_sum ( ): It returns the sum of values in an array as integer or float.
Syntax:
array_sum ($array)
Example of array sum:
<!DOCTYPE html>
<html>
<head>
<title>foreach example</title>
</head>
<body>
        <?php
        $a=array (1,2,3,4); //integer number
        echo "sum(a)=".array_sum($a);
        $b=array (1.2,2.3); //float number
        echo "sum(b)=".array_sum($b);
        ?>
 </body>
</html>
output:
sum(a)=10sum(b)=3.5

2. array_pop ( ): It delete the last element of an array.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array ("html","css","php");
array_pop($a); // It will delete php because php is the last element
print_r($a);
?>
</body>
</html>
output:
Array ( [0] => html [1] => css )

3. array_push: The array_push() function inserts one or more elements to the end of an array.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("html","css","php");
array_push($a,"java","javascript"); //add java and javascript at the end of array.
print_r($a)
?>
</body>
</html>
output:
Array ( [0] => html [1] => css [2] => php [3] => java [4] => javascript )

4. array_rand( ):  Returns one or more random keys from an array.
syntax:
array_rand(array,number)
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$input = array("html", "css", "php", "java", "javascript");
$rand_keys = array_rand($input, 2); //any two subject will be showed on the output
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
</body>
</html>
output:
html javascript
//or
html css

5. array_reverse( ):It returns an array in reverse order.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a= array("html", "css", "php", "java", "javascript");
print_r(array_reverse($a));
?>
</body>
</html>
output:
Array ( [0] => javascript [1] => java [2] => php [3] => css [4] => html )

6. array_shift: It remove the first element of the array and return the remove element.
Example:
<!DOCTYPE html>
<html>
<body>


<?php
$a= array("html", "css", "php", "java", "javascript");
echo array_shift($a);
?>
</body>
</html>
output:
html

7. array_unshift( ): It add one or more element at the beginning of an array.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a= array("html", "css", "php", "java", "javascript");
echo array_unshift($a, "c++" , ".net");
print_r($a);
?>
</body>
</html>
output:
7Array ( [0] => c++ [1] => .net [2] => html [3] => css [4] => php [5] => java [6] => javascript )

8. asort: It is an associative array in ascending order, according to the value.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$age=array("nandon"=>"22","gyandeep"=>"21","vinoy"=>"23");
asort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>


output:
Key=gyandeep, Value=21
Key=nandon, Value=22
Key=vinoy, Value=23

9. arsort( ): It is an associative array in descending order, according to the value.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$age=array("nandon"=>"22","gyandeep"=>"21","vinoy"=>"23");
arsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>
output:
Key=vinoy, Value=23
Key=nandon, Value=22
Key=gyandeep, Value=21

10. array_product( ): It calculate and return the product of an array.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array(10,5);
echo(array_product($a));
?>
</body>
</html>
output:
50
Conclusion: Here in this article we see some important array function with examples.
For more information visit our website: http://www.admecindia.co.in/php-master-course.html

No comments:

Post a Comment

Featured Post

ADMEC Multimedia Institute Scholarship Program

The ADMEC Multimedia Institute scholarship program aims to select and trained talented non working female, married woman and non married m...