<?php
/* Usage:
/*
/* $report = new Reports_Core; (In Kohana just new Reports;)
/* print $report->csv($result_set_or_array, 'a title', ';');
/*
/*--------------------------------------------------------------*/
class Reports_Core
{
function csv($lines = FALSE, $title = '', $delimiter = ';')
{
$output = '';
if($title) $output .= $title."
";
if($lines)
{
$report_line = array();
foreach($lines as $line)
{
$line = implode($delimiter, (array) $line);
$report_line[] = $line."
";
}
$output .= implode('', $report_line);
return $output;
}
else
{
trigger_error('There is no data to generate a CSV report.', E_USER_ERROR);
}
}
}
?>
Tag Archive for array
CSV Reports
get first key of associative array
echo key($array);
get first value of an associative array
echo reset($array);
Array hacks
class Array
# order the elements of this array by the values of the specified attribute of
# each element
#
def sort_by_attribute! atb
sort! {|a,b| a.send(atb) <=> b.send(atb)}
end
# provides the first element of this array for which the block provides "true"
#
def first_where
each do |element|
return element if yield element
end
nil
end
def index_of_first_where
each_with_index do |element,i|
return i if yield element
end
end
end
Array2object
function array2object(array $array) {
$object = new stdClass();
foreach($array as $key => $value) {
if(is_array($value)) {
$object->$key = array2object($value);
} else {
$object->$key = $value;
}
}
return $object;
}
Multidimensional Array
var sDataArray=MultiDimensionalArray(7,2);
sDataArray[6][1] = "This has been stored successfully!";
alert(sDataArray[6][1]);
function MultiDimensionalArray(iRows,iCols)
{
var a = new Array(iRows);
for (var i=0; i < iRows; i++)
{
a[i] = new Array(iCols);
for (var j=0; j < iCols; j++)
{
a[i][j] = "";
}
}
return(a);
}
// THREE DIMENSIONS
function MultiDimensionalArray(iRows,iCols,iDepth)
{
var a = new Array(iRows);
for (var i=0; i < iRows; i++)
{
a[i] = new Array(iCols);
for (var j=0; j < iCols; j++)
{
a[i][j] = new Array(iDepth);
for (var m=0; m < iDepth; m++)
{
a[i][j][m] = "";
}
}
}
return(a);
}
Category: Uncategorized |
Tags: array
in_array compact version
var in_array = function(s,a) {for(var k in a) if(a[k]==s) return 1; return 0;}
array sorting
// sorting the list by comparing the "date" property on the objects in the array NSSortDescriptor *dateSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO selector:@selector(compare:)] autorelease]; [list sortUsingDescriptors:[NSArray arrayWithObjects:dateSortDescriptor, nil]];
Convert Byte Array to String
byte [] dBytes = { 1, 2, 3, 4, 5}
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);