Tag Archives: Array

end

It sets the internal pointer of an array to its last element

Example

<?php
$array = array(“php”, “asp”, “jsp”, “as”, “js”);
echo end($array);
?>

Output

js

each

It return the current key and value pair from an array and advance the array cursor

Example

<?php
echo “<pre>”;
$array = array(“php”, “asp”, “jsp”, “as”, “js”);
$array2 = each($array);
print_r($array2);
?>

Output

Array
(
[1] => php
[value] => php
[0] => 0
[key] => 0
)

compact

it create array containing variables and their values

Example

<?php
echo “<pre>”;
$web  = “PHP”;
$clinet = “Brwoser”;
$vars = array(“clinet”);
$array = compact(“web”, “nothing_here”, $vars);
print_r($array);
?>

Output

Array
(
[web] => PHP
[clinet] => Brwoser
)

asort

It sort an array and maintain index association

Example

<?php
echo “<pre>”;
$array = array(‘PHP’,’ ASP’, ‘JS’,’AS’);
asort($array);
print_r($array);
?>

Output

Array
(
[1] =>  ASP
[3] => AS
[2] => JS
[0] => PHP
)

arsort

It sort an array in reverse order and maintain index association

Example

<?php
echo “<pre>”;
$array = array(‘PHP’,’ ASP’, ‘JS’,’AS’);
arsort($array);
print_r($array);
?>

Output

Array
(
[0] => PHP
[2] => JS
[3] => AS
[1] =>  ASP
)

array

It can be used to create an array

Example

<?php
echo “<pre>”;
$array = array(‘PHP’,’ ASP’, ‘JS’,’AS’);
print_r($array);
?>

Output

Array
(
[0] => PHP
[1] =>  ASP
[2] => JS
[3] => AS
)

array_walk

It apply a user function to every member of an array

Example

<?php
function func($value,$key){
echo “The key $key has the value $value<br />”;
}
$arr1=array(“p”=>”php”,”a”=>”ASP”);
array_walk($arr1,”func”);
?>

Output

The key p has the value php
The key a has the value ASP

array_walk_recursive

It applay a user function recursively to every member of an array

Example

<?php
function func($value,$key){
echo “The key $key has the value $value<br />”;
}
$arr1=array(“p”=>”php”,”a”=>”ASP”);
$arr2=array($arr1,”1″=>”JS”,”2″=>”VS”);
array_walk_recursive($arr2,”func”);
?>

Output

The key p has the value php
The key a has the value ASP
The key 1 has the value JS
The key 2 has the value VS