Tag Archive for array

CSV Reports

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

}
}

?>

source

get first key of associative array

echo key($array);

source

get first value of an associative array

echo reset($array);

source

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

source

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;
}

source

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);
}

source

convert string to list of words

private static List<string> stringToWordArray(string s)
{
List<string> words = new List<string>();
string word;
s = s.ToUpper();
s = s.Trim(',', '.', '"', '-', ' ', ':', ';', '
', '!', '?');
while (s.Length > 0)
{
if (s.Contains(' '))
{
word = s.Substring(0, s.IndexOf(" "));
s = s.Substring(s.IndexOf(" ") + 1);
s = s.Trim();
}
else
{
word = s;
s = "";
}
word = word.Trim(',', '.', '"', '-', ' ', ':', ';', '
', '!', '?');
if (word.Length > 0)
words.Add(word);
else
return words;

}
return words;
}

source

in_array compact version

var in_array = function(s,a) {for(var k in a) if(a[k]==s) return 1; return 0;}

source

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]];

source

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

source