Tag Archives: Array

key

It fetches a key from an array

Example

<?php
$lan = array(1=>”PHP”,2=> “ASP”, 3=>”JSP”);
foreach($lan as $value)
if(“PHP”==$value)
echo key($lan);
?>

Output

2

extract

It import variables into the current symbol table from an array

Example

<?php
$array = array(“lan”=>”php”, “server”=> “Apache”, “browser”=> “Chrome”);
extract($array);
echo $lan.”<br >”;
echo $server.”<br >”;
echo $browser.”<br >”;
?>

Output

php
Apache
Chrome

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
)