PHP Snippets
array_pivot
Information
(PHP Version >= 3)Functions used in this snippet: is_array, count, is_null, array_key_exists, elseif.
Description
array array_pivot( array array, str key, str value )
array_pivot will take a multidimensional associative array and return a single dimension associative array with the keys from the column $key and the values from the column $value. If $key is set to null then it will return a regular array with integer keys.
Snippet
<?php
function array_pivot($array, $key, $value) {
if(!is_array($array))
return false;
if(count($array) === 0)
return array();
$ret = array();
foreach($array as $ray) {
if(is_null($key)) {
if(array_key_exists($value, $ray)) {
$ret[] = $ray[$value];
}
} elseif(array_key_exists($key, $ray) && array_key_exists($value, $ray)) {
$ret[$ray[$key]] = $ray[$value];
}
}
return $ret;
}
?>
Example
array_pivot Can be used in the following way:
<?php
$array = array(
array(
'id' => '1',
'name' => 'Scott',
'email' => 'fake@test.com'
),
array(
'id' => '4',
'name' => 'John',
'email' => 'john@test.com'
),
array(
'id' => '56',
'name' => 'Chris',
'email' => 'chris@test.com'
)
);
print_r(array_pivot($array, 'id', 'name'));
/*
Array
(
[1] => Scott
[4] => John
[56] => Chris
)
*/
print_r(array_pivot($array, null, 'email'));
/*
Array
(
[0] => fake@test.com
[1] => john@test.com
[2] => chris@test.com
)
*/
?>
array_select_key
Information
(PHP Version >= 3)Functions used in this snippet: array_intersect_key, array_fill_keys.
Description
array array_select_key( array array, array keys )
Returns any values from the array $array which have keys that are in the array $keys
Snippet
<?php
function array_select_key($array, $keys) {
return array_intersect_key($array, array_fill_keys($keys, 1));
}
?>
Example
array_select_key Can be used in the following way:
<?php
$post = array(
'name' => 'Scott',
'email' => 'fakeaddress@scott.com',
'phone' => '5555551212',
'submit' => 'Add Contact'
);
$wanted = array('name', 'email', 'phone');
$insert = array_select_key($post, $wanted);
print_r($insert);
/*
Array
(
[name] => Scott
[email] => fakeaddress@scott.com
[phone] => 5555551212
)
*/
?>
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
?>