<?php
$scores = array(88, 75, 91, 84);
list($maths, $english, $history, $biology) = $scores;
printf("
Maths: %d; English: %d; History: %d; Biology: %d;
\n", $maths, $english, $history, $biology);?>
Here is another tutorial on combining arrays
<?php
$colors = array('red', 'yellow', 'green');
$flavors = array('apple', 'banana', 'lime');
$tastes = array('sweet', 'sour');
$prices = array();
$name = 'lemon';
$arrays = array('name' => $name, 'prices' => $prices, 'flavors' => $flavors, 'tastes' => $tastes);
foreach ($arrays as $key => $value){
if($fruits = @array_combine($colors, $value)){
printf("
%s\n", var_export($fruits, TRUE));
}
else {
printf("
Couldn't combine \$colors and \$%s.
", $key);}
}
?>
Here is another tutorial:
<?php
function array_average($array)
{
$retval = FALSE;
if(is_array($array) && count($array))
{
$retval = array_sum($array) / count($array);
return $retval;
}
}
#test the function
$scores = array('Bill' => 87.5, 'Jan' => 94.8, 'Terry' => 80.0, 'Andy' => 91.5, 'Lisa' => 95.5);
printf("
There are %d scores, totaling %.2f and averaging %.2f.
", count($scores), array_sum($scores), array_average($scores));?>
here is a tutorial for getting a safe square root of any number
<?php
function array_display($array, $pre=FALSE)
{
$tag = $pre ? 'pre' : 'p';
printf("<%s>%s%s>\n", $tag, var_export($array, TRUE), $tag);
}
//the callback function safe_sqrt() is applied to each number form the $values arary in turn.
function safe_sqrt($num)
{
return sqrt(abs($num)) . ($num < 0 ? 'i' : ''); #imaginary number symbol added if the number is negative. cool beans!
}
$values = array(4, 8, -3, 0, 14, -4);
$roots = array_map('safe_sqrt', $values);
echo "
Values:
";array_display($values, TRUE);
echo "
Square roots:
";array_display($roots, TRUE);
?>
No comments:
Post a Comment