PHP Snippets
array_natcase
Information
(PHP Version >= 3)Functions used in this snippet: natcasesort, array_reverse.
Description
array array_natcase( str key, array array[, str ascdesc] )
Sorts the associative array array by the associative array key key with natcasesort. It maintains all keys and can be sorted ascending or descending by setting ascdesc to either asc, or desc. The function returns the newly array sorted array.
Snippet
<?php
function array_natcase($key, $array, $ascdesc='asc')
{
$temp = array();
$final = array();
foreach($array as $id=>$value)
{
$temp[$id] = $value[$key];
}
natcasesort($temp);
foreach($temp as $id=>$value)
{
$final[$id] = $array[$id];
}
if($ascdesc{0} === 'd')
{
$final = array_reverse($final, true);
}
return $final;
}
?>
Example
array_natcase Can be used in the following way:
<?php
$array = array(
'a'=>array('x'=>10, 'other'=>'egh'),
'q'=>array('x'=>9, 'other'=>'blah'),
'7'=>array('x'=>1, 'other'=>'rawr'),
'6'=>array('x'=>22, 'other'=>'abcd'));
$array = array_natcase('x', $array);
//outputs the array in the following order
// 7, q, a, 6
?>
print_array
Information
(PHP Version >= 4.0.1)Functions used in this snippet: str_pad, array_keys, is_array, gettype.
Description
void print_array( array array, void level )
Prints the values and types of an array similarly to print_r but includes the values types and the arrays are colored as well according to the array colors.
Note: You don't need to do anything with the level variable.
Snippet
<?php
function print_array($array, $level=0){
$num = $level === 0 ? 0 : 4;
$colors = array( 'bracket'=>'#999999',
'value'=>'#FF0000',
'array'=>'#009900',
'arrow'=>'#999999',
'type'=>'#0000FF',
'key'=>'#000000');
echo '<span style="color: ' . $colors['array'] . ';">';
echo "Array\n" . str_pad('', ($level * 4) + ($level * $num), " ") . "(\n";
$keys = array_keys($array);
foreach($keys as $key){
echo str_pad('', ($level * 4) + ($level * $num), " ");
echo ' <span style="color: ' . $colors['bracket'] . ';">';
echo '[<span style="color: ' . $colors['key'] . ';">' . $key . '</span>]';
echo '</span> <span style="color: ' . $colors['arrow'] . ';">=></span> ';
if (is_array($array[$key])){
print_array($array[$key], $level + 1);
}else{
echo ' <span style="color: ' . $colors['value'] . ';">' . $array[$key] . '</span>';
echo ' <span style="color: ' . $colors['type'] . ';">' . gettype($array[$key]) . "</span>\n";
}
}
echo str_pad('', ($level * 4) + ($level * $num), " ") . ")</span>\n";
}
?>
Example
print_array Can be used in the following way:
<?php
echo '<pre>'; // format to show whitespace
$array = array(1,1,1,2);
print_array($array); // prints out array, each with int as type
$another_array = array('wee', 'haha', array(1,2,3), array(array(null), array(true)));
print_array($another_array);
// prints out that array above.
?>
array_sum_assoc
Information
(PHP Version >= 3)Functions used in this snippet: is_array, sizeof, array_sum_assoc.
Description
int array_sum_assoc( array array )
Sum's up all the values in the array array associatively.
Snippet
<?php
function array_sum_assoc($array){
if(!is_array($array) || sizeof($array) === 0){
return false;
}
$count = 0;
foreach($array as $key=>$value){
if(is_array($value)){
$count += array_sum_assoc($value);
}else{
$count += $value;
}
}
return $count;
}
?>
Example
array_sum_assoc Can be used in the following way:
<?php
// make an associative array
$array = array(1,array(1),array(1,array(1)));
echo array_sum_assoc($array);
// outputs 4, array_sum would output 1
?>