Tag Archives: Array

prev

It rewinds the internal array pointer

Example

<?php
$lan = array(“PHP”,”JSP”,”ASP”);
next($lan);
echo prev($lan);
?>

Output

PHP

Share

natsort

It sorts an array using a “natural order” algorithm

Example

<?php
echo “<pre>”;
$lan = array(“PHP”,”JSP”,”ASP”);
natsort($lan);
print_r($lan);
?>

Output

Array
(
[2] => ASP
[1] => JSP
[0] => PHP
)

Share

natcasesort

It sorts an array using a case insensitive “natural order” algorithm

Example

<?php
echo “<pre>”;
$lan = array(“PHP”,”ASP”,”JSP”);
natcasesort($lan);
print_r($lan);
?>

Output

Array
(
[1] => ASP
[2] => JSP
[0] => PHP
)

Share

list

It assigns variables as if they were an array

Example

<?php
echo “<pre>”;
$lan = array(“PHP”,”ASP”,”JSP”);
list($lan1, $lan2, $lan3) = $lan;
echo $lan1.”<br>”;
echo $lan2.”<br>”;
echo $lan3.”<br>”;
?>

Output

PHP
ASP
JSP

Share

ksort

It sorts an array by key

Example

<?php
echo “<pre>”;
$lan = array(2=>”PHP”,1=> “ASP”, 3=>”JSP”);
ksort($lan);
print_r($lan);
?>

Output

Array
(
[1] => ASP
[2] => PHP
[3] => JSP
)

Share

krsort

It sorts an array by key in reverse order

Example

<?php
echo “<pre>”;
$lan = array(1=>”PHP”,2=> “ASP”, 3=>”JSP”);
krsort($lan);
print_r($lan);
?>

Output

Array
(
[3] => JSP
[2] => ASP
[1] => PHP
)

Share

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

Share

in_array

It checks if a value exists in an array

Example

<?php
$lan = array(“PHP”, “ASP”, “JSP”);
if (in_array(“PHP”, $lan)) {
echo “PHP exists”;
}
?>

Output

PHP exists

Share