/*1. array_udiff_assoc()*/
Array
(
[b] => green
[c] => blue
)
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"punainen","b"=>"vihreä","c"=>"sininen");
$a2=array("a"=>"punainen","b"=>"sininen","c"=>"vihreä");
$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);
?>
/*2. array_udiff_uassoc()*/
Array
(
[c] => blue
)
<?php
function myfunction_key($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
function myfunction_value($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"punainen","b"=>"vihreä","c"=>"sininen");
$a2=array("a"=>"punainen","b"=>"vihreä","c"=>"vihreä");
$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);
?>
/*3. array_uintersect()*/
Array
(
[c] => blue
)
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"punainen","b"=>"vihreä","c"=>"sininen");
$a2=array("a"=>"sininen","b"=>"musta","e"=>"sininen");
$result=array_uintersect($a1,$a2,"myfunction");
print_r($result);
?>
/*4. array_uintersect_assoc()*/
Array
(
[a] => red
[b] => green
[c] => blue
)
<?php
function myfunction($a,$b)
{
if ($a===$b);
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"punainen","b"=>"vihreä","c"=>"sininen");
$a2=array("a"=>"punainen","b"=>"sininen","c"=>"vihreä");
$result=array_uintersect_assoc($a1,$a2,"myfunction");
print_r($result);
?>
/*5. array_uintersect_uassoc()*/
Array
(
[a] => red
[b] => green
)
<?php
function myfunction_key($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
function myfunction_value($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"punainen","b"=>"vihreä","c"=>"sininen");
$a2=array("a"=>"punainen","b"=>"vihreä","c"=>"vihreä");
$result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);
?>
/*6. each()*/
Array
(
[1] => Peter
[value] => Peter
[0] => 0
[key] => 0
)
<?php
$people = array("Petteri", "Joni", "Ken", "Kari");
print_r (each($people));
?>
/*7. end()*/
Peter
Cleveland
<?php
$people = array("Petteri", "Joni", "Ken", "Kari");
echo current($people) . "
";
echo end($people);
?>
/*8. extract ()*/
$a = Cat; $b = Dog; $c = Horse
<?php
$a = "Original";
$my_array = array("a" => "Kissa","b" => "Koira", "c" => "Hevonen");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
/*9. in_array()*/
Match found
<?php
$people = array("Petteri", "Joni", "Ken", "Kari");
if (in_array("Glenn", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
/*10. key()*/
The key from the current position is: 0
<?php
$people=array("Petteri","Joni","Ken","Kari");
echo "The key from the current position is: " . key($people);
?>
/*11. krsort()*/
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37
<?php
$age=array("Petteri"=>"35","Pena"=>"37","Joni"=>"43");
krsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
?>
/*12. ksort()*/
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
<?php
$age=array("Petteri"=>"35","Pena"=>"37","Joni"=>"43");
ksort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
?>
/*13. list()*/
I have several animals, a Dog, a Cat and a Horse.
<?php
$my_array = array("Koira","Kissa","Hevonen");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
/*14. natcasesort()*/
Natural order: Array
(
[1] => Temp10.txt
[3] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[0] => temp15.txt
)
Natural order case insensitve: Array
(
[2] => temp1.txt
[4] => temp2.txt
[1] => Temp10.txt
[0] => temp15.txt
[3] => Temp22.txt
)
<?php
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "
";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
?>
/*15. natsort()*/
Standard sorting: Array
(
[0] => temp1.txt
[1] => temp10.txt
[2] => temp15.txt
[3] => temp2.txt
[4] => temp22.txt
)
Natural order: Array
(
[0] => temp1.txt
[3] => temp2.txt
[1] => temp10.txt
[2] => temp15.txt
[4] => temp22.txt
)
<?php
$temp_files = array("temp15.txt","temp10.txt",
"temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "Standard sorting: ";
print_r($temp_files);
echo "
";
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
?>
/*16. rsort()*/
Volvo
Toyota
BMW
<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "
";
}
?>
/*17. shuffle()*/
Array
(
[0] => blue
[1] => red
[2] => yellow
[3] => purple
[4] => green
)
<?php
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array);
?>
/*18. sizeof()*/
3
<?php
$cars=array("Volvo","BMW","Toyota");
echo sizeof($cars);
?>
/*19. sort()*/
BMW
Toyota
Volvo
<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "
";
}
?>
/*20. uasort()*/
Key=b, Value=2
Key=a, Value=4
Key=d, Value=6
Key=c, Value=8
<?php
function my_sort1($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uasort($arr,"my_sort1");
foreach($arr as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
?>
/*21. uksort()*/
Key=a, Value=4
Key=b, Value=2
Key=c, Value=8
Key=d, Value=6
<?php
function my_sort2($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uksort($arr,"my_sort2");
foreach($arr as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
?>
/*22. usort()*/
2
4
6
8
<?php
function my_sort3($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
usort($a,"my_sort3");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++)
{
echo $a[$x];
echo "
";
}
?>