Tag Archive for array

Add Item to Array

// Add an item to the end of an existing array
string[] ar1 = new string[] {"I", "Like", "To"}

// create a temporary array with an extra slot
// at the end
string[] ar2 = new string[ar1.Length + 1];

// add the contents of the ar1 to ar2
// at position 0
ar1.CopyTo(ar2, 0);

// add the desired value
ar2.SetValue("Code.", ar1.Length);

// overwrite ar1 with ar2 and voila!
// the contents of ar1 should now be {"I", "Like", "To", "Code."}
ar1 = ar2;

source

Merging a String Array to a Comma delimited String (foreach)

string str = "";

// loop though an array and create a string
// delimited with commas
foreach (string s in fieldNames)
{
str += s;

// if this not the last item in the array
// append a comma to end of the string.
if (s != fieldNames[fieldNames.Length - 1])
{
str += ", ";
}
}

source

Collection class

<?php
class Collection {
private $items;
private $attributes;

public function __construct() {
$this->items = array();
$this->attributes = array();
$this->attributes['Count'] = 0;
$this->attributes['IsFixedSize'] = false;
$this->attributes['FixedSize'] = 0;
$this->attributes['IsReadOnly'] = false;
}

public function __get($var) {
if(key_exists($var, (array)$this->attributes)) {
return $this->attributes[$var];
}
else {
throw new Exception("The property {$var} does not exist", 0);
}
}

public function __set($var, $value) {
if(key_exists($var, (array)$this->attributes)) {
$this->attributes[$var] = $value;
}
else {
throw new Exception("The property {$var} cannot be set as it does not exist", 0);
}
}

public function Add($item) {
if($this->IsFixedSize) {
if($this->Count < $this->FixedSize) {
$this->items[] = $item;
$this->Count += 1;
}
else {
throw new Exception("Cannot not add more items to collection. Max size is {$this->FixedSize}", 0);
}
}
else if($this->IsReadOnly) {
throw new Exception("Cannot add item to a read only collection", 0);
}
else {
$this->items[] = $item;
$this->Count += 1;
}
}

public function AddRange(array $items) {
foreach($items as $item) {
self::Add($item);
}
}

public function Contains($item) {
foreach($this->items as $i) {
if($i == $item) {
return true;
}
}
return false;
}

public function Get($index) {
if(key_exsits($index, $this->items)) {
return $this->items[$index];
}
return false;
}

public function GetCollectionAsArray() {
return $this->items;
}

public function GetCollectionAsString() {
return implode(", ", $this->items);
}

public function IndexOf($item, $startIndex = 0) {
for($i = $startIndex; $i < $this->Count; $i++) {
if($this->items[$i] == $item) {
return $i;
break;
}
}
return -1;
}

public function LastIndexOf($item) {
$lastIndex = -1;

for($i = 0; $i < $this->Count; $i++) {
if($this->items[$i] == $item) {
$lastIndex = $i;
}
}
return $lastIndex;
}

public function Insert($index, $item) {
if($this->IsFixedSize) {
if($index < $this->FixedSize) {
$this->items[$index] = $item;
$this->Count += 1;
}
else {
throw new Exception("Cannot insert item at {$index}. Max size is {$this->FixedSize}", 0);
}
}
else if($this->IsReadOnly) {
throw new Exception("Cannot insert an item into a read only collection", 0);
}
else {
$this->items[$index] = $item;
$this->Count += 1;
}
}

public function Remove($item) {
$index = self::IndexOf($item);
self::RemoveAt($index);
}

public function RemoveAt($index) {
if(!$this->IsReadOnly) {
if(key_exists($index, $this->items)) {
unset($this->items[$index]);
$this->Count -= 1;
}
else {
throw new Exception("Index out of range. The index {$index} is out of range of the collection", 0);
}
}
else {
throw new Exception("Cannot remove item from read only collection", 0);
}
}

public function RemoveRange($startIndex, $endIndex) {
for($i = $startIndex; $i < $endIndex; $i++) {
self::RemoveAt($i);
}
}

public function Sort() {
sort($this->items, SORT_STRING);
}
}
?>

source

Push a new member into an array that is contained in a hash value

# append new members to an existing hash of arrays
push @{ $HoA{"simpsons"} }, "lisa", "maggie";

source

in_array()

function in_array( what, where ){
var a=false;
for(var i=0;i<where.length;i++){
if(what == where[i]){
a=true;
break;
}
}
return a;
}

source

Array of Countries in Turkish.

$countries = Array
(
[VI] => ABD Virgin Adaları
[AF] => Afganistan
[AX] => Aland Adaları
[DE] => Almanya
[US] => Amerika BirleÅŸik Devletleri
[UM] => Amerika Birleşik Devletleri Küçük Dış Adaları
[AS] => Amerikan Samoası
[AD] => Andora
[AO] => Angola
[AI] => Anguilla
[AQ] => Antarktika
[AG] => Antigua ve Barbuda
[AR] => Arjantin
[AL] => Arnavutluk
[AW] => Aruba
[QU] => Avrupa BirliÄŸi
[AU] => Avustralya
[AT] => Avusturya
[AZ] => Azerbaycan
[BS] => Bahamalar
[BH] => Bahreyn
[BD] => BangladeÅŸ
[BB] => Barbados
[EH] => Batı Sahara
[BZ] => Belize
[BE] => Belçika
[BJ] => Benin
[BM] => Bermuda
[BY] => Beyaz Rusya
[BT] => Bhutan
[ZZ] => Bilinmeyen veya Geçersiz Bölge
[AE] => BirleÅŸik Arap Emirlikleri
[GB] => Birleşik Krallık
[BO] => Bolivya
[BA] => Bosna Hersek
[BW] => Botsvana
[BV] => Bouvet Adası
<br /> => Brezilya
[BN] => Brunei
[BG] => Bulgaristan
[BF] => Burkina Faso
[BI] => Burundi
[CV] => Cape Verde
[GI] => Cebelitarık
[DZ] => Cezayir
[CX] => Christmas Adası
[DJ] => Cibuti
[CC] => Cocos Adaları
[CK] => Cook Adaları
[TD] => Çad
[CZ] => Çek Cumhuriyeti
[CN] => Çin
[DK] => Danimarka
[DM] => Dominik
[DO] => Dominik Cumhuriyeti
[TL] => DoÄŸu Timor
[EC] => Ekvator
[GQ] => Ekvator Ginesi
[SV] => El Salvador
[ID] => Endonezya
[ER] => Eritre
[AM] => Ermenistan
[EE] => Estonya
[ET] => Etiyopya
[FK] => Falkland Adaları (Malvinalar)
[FO] => Faroe Adaları
[MA] => Fas
[FJ] => Fiji
[CI] => FildiÅŸi Sahilleri
[PH] => Filipinler
[PS] => Filistin Bölgesi
[FI] => Finlandiya
[FR] => Fransa
[GF] => Fransız Guyanası
[TF] => Fransız Güney Bölgeleri
[PF] => Fransız Polinezyası
[GA] => Gabon
[GM] => Gambia
[GH] => Gana
[GN] => Gine
[GW] => Gine-Bissau
[GD] => Granada
[GL] => Grönland
[GP] => Guadeloupe
[GU] => Guam
[GT] => Guatemala
[GG] => Guernsey
[GY] => Guyana
[ZA] => Güney Afrika
[GS] => Güney Georgia ve Güney Sandwich Adaları
[KR] => Güney Kore
[CY] => Güney Kıbrıs Rum Kesimi
[GE] => Gürcistan
[HT] => Haiti
[HM] => Heard Adası ve McDonald Adaları
[IN] => Hindistan
[IO] => Hint Okyanusu İngiliz Bölgesi
[NL] => Hollanda
[AN] => Hollanda Antilleri
[HN] => Honduras
[HK] => Hong Kong SAR - Çin
<hr /> => Hırvatistan
[IQ] => Irak
[VG] => İngiliz Virgin Adaları
[IR] => İran
[IE] => İrlanda
[ES] => İspanya
[IL] => İsrail
[SE] => İsveç
[CH] => İsviçre
[IT] => İtalya
[IS] => İzlanda
[JM] => Jamaika
[JP] => Japonya
[JE] => Jersey
[KH] => Kamboçya
[CM] => Kamerun
[CA] => Kanada
[ME] => KaradaÄŸ
[QA] => Katar
[KY] => Kayman Adaları
[KZ] => Kazakistan
[KE] => Kenya
[KI] => Kiribati
[CO] => Kolombiya
[KM] => Komorlar
[CG] => Kongo
[CD] => Kongo Demokratik Cumhuriyeti
[CR] => Kosta Rika
[KW] => Kuveyt
[KP] => Kuzey Kore
[MP] => Kuzey Mariana Adaları
[CU] => Küba
[KG] => Kırgızistan
[LA] => Laos
[LS] => Lesotho
[LV] => Letonya
[LR] => Liberya
[LY] => Libya
[LI] => Liechtenstein
[LT] => Litvanya
[LB] => Lübnan
[LU] => Lüksemburg
[HU] => Macaristan
[MG] => Madagaskar
[MO] => Makao S.A.R. Çin
[MK] => Makedonya
[MW] => Malavi
[MV] => Maldivler
[MY] => Malezya
[ML] => Mali
[MT] => Malta
[IM] => Man Adası
[MH] => Marshall Adaları
[MQ] => Martinik
[MU] => Mauritius
[YT] => Mayotte
[MX] => Meksika
[FM] => Mikronezya Federal Eyaletleri
[MD] => Moldovya Cumhuriyeti
[MC] => Monako
[MS] => Montserrat
[MR] => Moritanya
[MZ] => Mozambik
[MN] => MoÄŸolistan
[MM] => Myanmar
[EG] => Mısır
[NA] => Namibya
[NR] => Nauru
[NP] => Nepal
[NE] => Nijer
[NG] => Nijerya
[NI] => Nikaragua
[NU] => Niue
[NF] => Norfolk Adası
[NO] => Norveç
[CF] => Orta Afrika Cumhuriyeti
[UZ] => Özbekistan
[PK] => Pakistan
[PW] => Palau
[PA] => Panama
[PG] => Papua Yeni Gine
[PY] => Paraguay
[PE] => Peru
[PN] => Pitcairn
[PL] => Polonya
[PT] => Portekiz
[PR] => Porto Riko
[RE] => Reunion
[RO] => Romanya
[RW] => Ruanda
[RU] => Rusya Federasyonu
[SH] => Saint Helena
[KN] => Saint Kitts ve Nevis
[LC] => Saint Lucia
[PM] => Saint Pierre ve Miquelon
[VC] => Saint Vincent ve Grenadinler
[WS] => Samoa
[SM] => San Marino
[ST] => Sao Tome ve Principe
[SN] => Senegal
[SC] => SeyÅŸeller
[SL] => Sierra Leone
[SG] => Singapur
[SK] => Slovakya
[SI] => Slovenya
[SB] => Solomon Adaları
[SO] => Somali
[LK] => Sri Lanka
[SD] => Sudan
[SR] => Surinam
[SY] => Suriye
[SA] => Suudi Arabistan
[SJ] => Svalbard ve Jan Mayen
[SZ] => Svaziland
[RS] => Sırbistan
[CS] => Sırbistan-Karadağ
[CL] => Åžili
[TJ] => Tacikistan
[TZ] => Tanzanya
[TH] => Tayland
[TW] => Tayvan
[TG] => Togo
[TK] => Tokelau
[TO] => Tonga
[TT] => Trinidad ve Tobago
[TN] => Tunus
[TC] => Turks ve Caicos Adaları
[TV] => Tuvalu
[TR] => Türkiye
[TM] => Türkmenistan
[UG] => Uganda
[UA] => Ukrayna
[OM] => Umman
[UY] => Uruguay
[QO] => Uzak Okyanusya
[JO] => Ürdün
[VU] => Vanuatu
[VA] => Vatikan
[VE] => Venezuela
[VN] => Vietnam
[WF] => Wallis ve Futuna
[YE] => Yemen
[NC] => Yeni Kaledonya
[NZ] => Yeni Zelanda
[GR] => Yunanistan
[ZM] => Zambiya
[ZW] => Zimbabve
)

source

Passing an array through GET request

Here are two options for passing an array via GET parameters (in a url):

<A href="example.html?arr[]=val1&arr[]=val2&arr[]=val3">test</A>

<A href="example.html?arr=<?PHP echo serialize($arr); ?>">test</A>

In the first example, you can use $_GET["arr"] as an array.
In the second you will first have to:

<?PHP
$arr = unserialize($_GET["arr"]);
?>

source

strstr() and stristr() with Arrays

<?php

function strstr_array( $haystack, $needle ) {
if ( !is_array( $haystack ) ) {
return false;
}
foreach ( $haystack as $element ) {
if ( strstr( $element, $needle ) ) {
return $element;
}
}
}

function stristr_array( $haystack, $needle ) {
if ( !is_array( $haystack ) ) {
return false;
}
foreach ( $haystack as $element ) {
if ( stristr( $element, $needle ) ) {
return $element;
}
}
}

?>

source

php while statement associative array from mysql

while($row = mysql_fetch_assoc($res)) {
}

source

strip HTML

// s : la chaine de caractère - tags : la liste des tags à supprimer
private function stripHTML(s:String, if(tags == undefined) tags = ["p","h1","h2","h3","h4","h5","h6","strong","em","abbr","acronym","address","bdo","blockquote","cite","q","code","ins","del","dfn","kbd","pre","samp","var","br","a","base","ul","ol","li","dl","dt","dd","table","tr","td","th","tbody","thead","tfoot","col","colgroup","caption","script","noscript","html","head","body","div","span","a","abbr","acronym","address","area","b","base","bdo","big","blockquote","body","br","button","caption","cite","code","col","colgroup","dd","del","dfn","div","dl","DOCTYPE","dt","em","fieldset","form","h1","h2","h3","h4","h5","andh6","head","html","hr","i","img","input","ins","kbd","label","legend","li","link","map","meta","noscript","object","ol","optgroup","option","p","param","pre","q","samp","script","select","small","span","strong","style","sub","sup","table","tbody","td","textarea","tfoot","th","thead","title","tr","tt","ul","var","DOCTYPE","title","link","meta","style","b","i","tt","sub","sup","big","small","hr"];
for (var i = 0; i < tags.length; i++) {
var tag:Array = ["<"+tags[i]">", "</"+tags[i]">", "<"+tags[i]"/>", "<"+tags[i]" />"];
for (var j = 0; j<tag.length; j++) {
var tmp = s.split(tag[j]);
s = tmp.join(" ");
}
}
return s;
}
//

source
tags: as