Tuesday, May 24, 2011

Here is a tutorial on using multisort for arrays. it is quite complex I think.


<?php
$data = array(
array('country'=>'Spain', 'language'=>'Spanish', 'visitors'=>1289),
array('country'=>'France', 'language'=>'French', 'visitors'=>984),
array('country'=>'Argentina','language'=>'Spanish','visitors'=>812),
array('country'=>'UK', 'language'=>'English', 'visitors'=>2786),
array('country'=>'Germany', 'language'=>'German', 'visitors'=>2786),
array('country'=>'Canada', 'language'=>'English', 'visitors'=>2331),
array('country'=>'Austria', 'language'=>'German', 'visitors'=>1102),
array('country'=>'Mexico', 'language'=>'Spanish', 'visitors'=>1071)
);
$cols = array();

foreach($data as $row)
{
foreach($row as $key => $value)
{
if(!isset($cols[$key]))
$cols[$key] = array();
$cols[$key][] = $value;
}
}
$data = $cols;
array_multisort($data['language'], $data['country'], $data['visitors']);

printf("<pre>%s</pre>\n", var_export($data, TRUE));
?>


here is another tutorial dealing with multisort


<?php
$eng = array('one', 'two', 'three', 'four'); #1-4 in English
$esp = array('uno', 'dos', 'tres', 'quatro'); #1-4 in Spanish
$deu = array('eins', 'zwei', 'drei', 'vier'); #1-4 in German
$rus = array('odin', 'dva', 'tri', 'chetire'); #1-4 in Russian
$digits = range(1,4);
array_multisort($rus, $esp, $deu, $eng, $digits);

foreach(range(0, 3) as $j){
printf("

Russian: %s (%d); Spanish: %s; German: %s; English: %s.

", $rus[$j], $digits[$j], $esp[$j], $deu[$j], $eng[$j]);
}
?>

Monday, May 16, 2011


<?php

$nums = array(15, 2.2, -4, 2.3, 0);
asort($nums);
printf("
%s
\n", var_export($nums, TRUE));
ksort($nums);
printf("
%s
\n", var_export($nums, TRUE));
?>


Here is another tutorial using krsort to sort an associative array


<?php
$dogs = array('Lassie' => 'Collie', 'Bud' => 'Sheepdog', 'Rin-Tin-Tin' => 'Alsatian', 'Snoopy' => 'Beagle');
krsort($dogs);
printf("<pre>%s</pre>\n", var_export($dogs, TRUE));
?>


Here is a tutorial about shuffling arrays


<?php
$nums = array(15, 2.2, -4, 17, 2.9, 0, 14);

shuffle($nums); //shuffles the array in random order
printf("<pre>%s</pre>\n", var_export($nums, TRUE));

shuffle($nums);
printf("<pre>%s</pre>\n", var_export($nums, TRUE));

shuffle($<
%s<
\n", var_export($nums, TRUE));
?>

Monday, May 9, 2011


<?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\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);

?>