Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

PHP Array Built

The document discusses several PHP array functions: - array_product() calculates the product of all elements in an array. - array_slice() extracts a slice of the array between given indices. - array_splice() removes elements and adds new elements, replacing the existing elements. - array_unique() removes duplicate values in an array, keeping the first occurrence.

Uploaded by

Kin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

PHP Array Built

The document discusses several PHP array functions: - array_product() calculates the product of all elements in an array. - array_slice() extracts a slice of the array between given indices. - array_splice() removes elements and adds new elements, replacing the existing elements. - array_unique() removes duplicate values in an array, keeping the first occurrence.

Uploaded by

Kin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP array_product() Function

There are some point of time when we need to calculate the product of all elements in an array.
The most basic way to do this is to iterate over all elements
and calculate the product but PHP serves us with a builtin function to do this. The
array_product() is a built-in function in PHP and is used to find the products of all the elements
in an array.
<?php
// PHP function to illustrate the use
// of array_product()
// Return Integer number
function Product($array)
{
$result = array_product($array);
return($result);
} $array = array(5, 8, 9, 2, 1, 3, 6);
print_r(Product($array));
?>
PHP array_slice() Function

The array_slice() is an inbuilt function of PHP and is used to fetch a part of an array by slicing
through it, according to the users choice.
<?php
// PHP program to illustrate the
// array_slice() function
// Input array
$array = array("ram", "krishna", "aakash",
"gaurav", "raghav");
// Slice from pos 1 to pos 3

print_r(array_slice($array, 1, 3, true));
?>
PHP array_splice() Function

This inbuilt function of PHP is an advanced and extended version of array_slice() function,
where we not only can remove elements from an array but can also add other elements to the
array. The
function generally replaces the existing element with elements from other arrays and returns an
array of removed or replaced elements.
PHP:
<?php
// PHP program to illustrate the use
// of array_splice() function
$array1 = array("10"=>"raghav", "20"=>"ram",
"30"=>"laxman","40"=>"aakash","50"=>"ravi");
$array2 = array("60"=>"ankita","70"=>"antara");
echo "The returned array: \n";
print_r(array_splice($array1, 1, 4, $array2));
echo "\nThe original array is
modified to: \n";
print_r($array1);
?>
PHP array unique() Function
The array unique() is a built-in function in PHP and this function removes duplicate values from
an array. If there are multiple elements in the array with same values then
the first appearing element will be kept and all other occurrences of this element will be removed
from the array.
<?php
// Input Array
$a=array("red", "green", "red", "blue");
// Array after removing duplicates
print_r(array_unique($a));
?>
PHP array_map() Function
The array_map() function sends each value of an array to a user-made function, and returns an
array with new values, given by the user-made function.
<?php
function myfunction($v)
{
return($v*$v);
}

$a=array(1,2,3,4,5);
print_r(array_map("myfunction",$a));
?>

PHP current() Function


The current() function is an inbuilt function in PHP.
It is used to return the value of the element in an array which the internal pointer is currently
pointing to. value.
In PHP, all arrays have an internal pointer. This internal pointer points to some element in that
array which is called as the current element of the array .Usually, the current element is the first
inserted element in the array.
<?php
// input array
$arr = array("Ram", "Shita", "Geeta");

// Here current function returns the

// first element of the array.


echo current($arr);
?>
PHP extract() Function

The extract() Function is an inbuilt function in PHP. The extract() function does array to
variable conversion. That is it converts array keys into variable names and array values into
variable value. In other words, we can say that the extract() function imports variables from an
array to the symbol table.
<?php

// input array
$state = array("AS"=>"ASSAM", "OR"=>"ORISSA", "KR"=>"KERALA");

extract($state);

// after using extract() function


echo"\$AS is $AS\n\$KR is $KR\n\$OR is $OR";

?>
PHP ksort() Function
The ksort() function sorts an associative array in ascending order, according to the key. Use the
krsort() function to sort an associative array in descending order, according to the key.
<?php

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

ksort($age);

?>
PHP array _flip() Function

The array_flip() function flips/exchanges all keys with their associated values in an array.

<?php

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");

$result=array_flip($a1);

print_r($result);

?>
PHP array_pad() Function
The array pad() function inserts a specified number of elements, with a specified value, to an array. This
function will not delete any elements if the size parameter is less than the size of the original array

<?php
$a=array("red","green");

print_r(array_pad($a,5,"blue"));

?>

You might also like