Tag Archive for array

Form — country code generated from an array of countries + codes

<?php
$countries = array(
  "AU" => "Australia",
  "AF" => "Afghanistan",
  "AL" => "Albania",
  "DZ" => "Algeria",
  "AS" => "American Samoa",
  "AD" => "Andorra",
  "AO" => "Angola",
  "AI" => "Anguilla",
  "AQ" => "Antarctica",
  "AG" => "Antigua & Barbuda",
  "AR" => "Argentina",
  "AM" => "Armenia",
  "AW" => "Aruba",
  "AT" => "Austria",
  "AZ" => "Azerbaijan",
  "BS" => "Bahamas",
  "BH" => "Bahrain",
  "BD" => "Bangladesh",
  "BB" => "Barbados",
  "BY" => "Belarus",
  "BE" => "Belgium",
  "BZ" => "Belize",
  "BJ" => "Benin",
  "BM" => "Bermuda",
  "BT" => "Bhutan",
  "BO" => "Bolivia",
  "BA" => "Bosnia/Hercegovina",
  "BW" => "Botswana",
  "BV" => "Bouvet Island",
  "BR" => "Brazil",
  "IO" => "British Indian Ocean Territory",
  "BN" => "Brunei Darussalam",
  "BG" => "Bulgaria",
  "BF" => "Burkina Faso",
  "BI" => "Burundi",
  "KH" => "Cambodia",
  "CM" => "Cameroon",
  "CA" => "Canada",
  "CV" => "Cape Verde",
  "KY" => "Cayman Is",
  "CF" => "Central African Republic",
  "TD" => "Chad",
  "CL" => "Chile",
  "CN" => "China, People's Republic of",
  "CX" => "Christmas Island",
  "CC" => "Cocos Islands",
  "CO" => "Colombia",
  "KM" => "Comoros",
  "CG" => "Congo",
  "CD" => "Congo, Democratic Republic",
  "CK" => "Cook Islands",
  "CR" => "Costa Rica",
  "CI" => "Cote d'Ivoire",
  "HR" => "Croatia",
  "CU" => "Cuba",
  "CY" => "Cyprus",
  "CZ" => "Czech Republic",
  "DK" => "Denmark",
  "DJ" => "Djibouti",
  "DM" => "Dominica",
  "DO" => "Dominican Republic",
  "TP" => "East Timor",
  "EC" => "Ecuador",
  "EG" => "Egypt",
  "SV" => "El Salvador",
  "GQ" => "Equatorial Guinea",
  "ER" => "Eritrea",
  "EE" => "Estonia",
  "ET" => "Ethiopia",
  "FK" => "Falkland Islands",
  "FO" => "Faroe Islands",
  "FJ" => "Fiji",
  "FI" => "Finland",
  "FR" => "France",
  "FX" => "France, Metropolitan",
  "GF" => "French Guiana",
  "PF" => "French Polynesia",
  "TF" => "French South Territories",
  "GA" => "Gabon",
  "GM" => "Gambia",
  "GE" => "Georgia",
  "DE" => "Germany",
  "GH" => "Ghana",
  "GI" => "Gibraltar",
  "GR" => "Greece",
  "GL" => "Greenland",
  "GD" => "Grenada",
  "GP" => "Guadeloupe",
  "GU" => "Guam",
  "GT" => "Guatemala",
  "GN" => "Guinea",
  "GW" => "Guinea-Bissau",
  "GY" => "Guyana",
  "HT" => "Haiti",
  "HM" => "Heard Island And Mcdonald Island",
  "HN" => "Honduras",
  "HK" => "Hong Kong",
  "HU" => "Hungary",
  "IS" => "Iceland",
  "IN" => "India",
  "ID" => "Indonesia",
  "IR" => "Iran",
  "IQ" => "Iraq",
  "IE" => "Ireland",
  "IL" => "Israel",
  "IT" => "Italy",
  "JM" => "Jamaica",
  "JP" => "Japan",
  "JT" => "Johnston Island",
  "JO" => "Jordan",
  "KZ" => "Kazakhstan",
  "KE" => "Kenya",
  "KI" => "Kiribati",
  "KP" => "Korea, Democratic Peoples Republic",
  "KR" => "Korea, Republic of",
  "KW" => "Kuwait",
  "KG" => "Kyrgyzstan",
  "LA" => "Lao People's Democratic Republic",
  "LV" => "Latvia",
  "LB" => "Lebanon",
  "LS" => "Lesotho",
  "LR" => "Liberia",
  "LY" => "Libyan Arab Jamahiriya",
  "LI" => "Liechtenstein",
  "LT" => "Lithuania",
  "LU" => "Luxembourg",
  "MO" => "Macau",
  "MK" => "Macedonia",
  "MG" => "Madagascar",
  "MW" => "Malawi",
  "MY" => "Malaysia",
  "MV" => "Maldives",
  "ML" => "Mali",
  "MT" => "Malta",
  "MH" => "Marshall Islands",
  "MQ" => "Martinique",
  "MR" => "Mauritania",
  "MU" => "Mauritius",
  "YT" => "Mayotte",
  "MX" => "Mexico",
  "FM" => "Micronesia",
  "MD" => "Moldavia",
  "MC" => "Monaco",
  "MN" => "Mongolia",
  "MS" => "Montserrat",
  "MA" => "Morocco",
  "MZ" => "Mozambique",
  "MM" => "Union Of Myanmar",
  "NA" => "Namibia",
  "NR" => "Nauru Island",
  "NP" => "Nepal",
  "NL" => "Netherlands",
  "AN" => "Netherlands Antilles",
  "NC" => "New Caledonia",
  "NZ" => "New Zealand",
  "NI" => "Nicaragua",
  "NE" => "Niger",
  "NG" => "Nigeria",
  "NU" => "Niue",
  "NF" => "Norfolk Island",
  "MP" => "Mariana Islands, Northern",
  "NO" => "Norway",
  "OM" => "Oman",
  "PK" => "Pakistan",
  "PW" => "Palau Islands",
  "PS" => "Palestine",
  "PA" => "Panama",
  "PG" => "Papua New Guinea",
  "PY" => "Paraguay",
  "PE" => "Peru",
  "PH" => "Philippines",
  "PN" => "Pitcairn",
  "PL" => "Poland",
  "PT" => "Portugal",
  "PR" => "Puerto Rico",
  "QA" => "Qatar",
  "RE" => "Reunion Island",
  "RO" => "Romania",
  "RU" => "Russian Federation",
  "RW" => "Rwanda",
  "WS" => "Samoa",
  "SH" => "St Helena",
  "KN" => "St Kitts & Nevis",
  "LC" => "St Lucia",
  "PM" => "St Pierre & Miquelon",
  "VC" => "St Vincent",
  "SM" => "San Marino",
  "ST" => "Sao Tome & Principe",
  "SA" => "Saudi Arabia",
  "SN" => "Senegal",
  "SC" => "Seychelles",
  "SL" => "Sierra Leone",
  "SG" => "Singapore",
  "SK" => "Slovakia",
  "SI" => "Slovenia",
  "SB" => "Solomon Islands",
  "SO" => "Somalia",
  "ZA" => "South Africa",
  "GS" => "South Georgia and South Sandwich",
  "ES" => "Spain",
  "LK" => "Sri Lanka",
  "XX" => "Stateless Persons",
  "SD" => "Sudan",
  "SR" => "Suriname",
  "SJ" => "Svalbard and Jan Mayen",
  "SZ" => "Swaziland",
  "SE" => "Sweden",
  "CH" => "Switzerland",
  "SY" => "Syrian Arab Republic",
  "TW" => "Taiwan, Republic of China",
  "TJ" => "Tajikistan",
  "TZ" => "Tanzania",
  "TH" => "Thailand",
  "TL" => "Timor Leste",
  "TG" => "Togo",
  "TK" => "Tokelau",
  "TO" => "Tonga",
  "TT" => "Trinidad & Tobago",
  "TN" => "Tunisia",
  "TR" => "Turkey",
  "TM" => "Turkmenistan",
  "TC" => "Turks And Caicos Islands",
  "TV" => "Tuvalu",
  "UG" => "Uganda",
  "UA" => "Ukraine",
  "AE" => "United Arab Emirates",
  "GB" => "United Kingdom",
  "UM" => "US Minor Outlying Islands",
  "US" => "USA",
  "HV" => "Upper Volta",
  "UY" => "Uruguay",
  "UZ" => "Uzbekistan",
  "VU" => "Vanuatu",
  "VA" => "Vatican City State",
  "VE" => "Venezuela",
  "VN" => "Vietnam",
  "VG" => "Virgin Islands (British)",
  "VI" => "Virgin Islands (US)",
  "WF" => "Wallis And Futuna Islands",
  "EH" => "Western Sahara",
  "YE" => "Yemen Arab Rep.",
  "YD" => "Yemen Democratic",
  "YU" => "Yugoslavia",
  "ZR" => "Zaire",
  "ZM" => "Zambia",
  "ZW" => "Zimbabwe"
);

$selected = ' selected="selected"';
$_country = NULL;
if ( isset($_GET['country']) ) {
    $_country = isset($countries[$_GET['country']]) ? $_GET['country'] : NULL;
}

echo "<select name="country">";
echo "<option>--- Please select your country --- </option>
";
foreach ( $countries as $code => $country ) {
    echo "<option value="$code"";
    if ( $_country == $code ) {
    echo $selected;
    }
    echo ">$country</option>
";
}
echo "</select>";
?>

source

Merge a group of arrays

/**
 * Merge a group of arrays
 *
 * @param array First array
 * @param array Second array
 * @param array Third array
 * @param array Etc...
 * @return array All array parameters merged into one
 */
	function amerge() 
	{
		$r = array();
		foreach(func_get_args()as $a) 
		{
			if (!is_array($a)) 
			{
				$a = array($a);
			}
			$r = array_merge($r, $a);
		}
		return $r;
	}

source

AS3 Search for a Value in Array and Return it’s Position Index

function findIndexInArray(value:Object, arr:Array):Number {
	for (var i:uint=0; i < arr.length; i++) {
		if (arr[i]==value) {
			return i;
		}
	}
	return NaN;
}

var myArray:Array = new Array("a","b","c","d","e","f","g");
trace(findIndexInArray("c", myArray));

// OUTPUT
// 2

source

AS3 Put Last Item of Array at the Beginning

function putLastItemOfArrayAtBeginning($a:Array):Array
{
	$a.unshift($a[$a.length-1]);
	$a.pop();
	return $a;
}

var myArray:Array = new Array(0,1,2,3,4,5);
trace(myArray);
myArray = putLastItemOfArrayAtBeginning(myArray);
trace(myArray);
myArray = putLastItemOfArrayAtBeginning(myArray);
trace(myArray);
myArray = putLastItemOfArrayAtBeginning(myArray);
trace(myArray);

// OUTPUT: 0,1,2,3,4,5
// OUTPUT: 5,0,1,2,3,4
// OUTPUT: 4,5,0,1,2,3
// OUTPUT: 3,4,5,0,1,2

source

AS3 Check if All the Values in Two Different Arrays are the Same

var array1:Array = new Array(1,2,3,4,5,6,7);
var array2:Array = new Array(1,2,3,9,5,6,7);

function checkIfArraysMatch($arrayA:Array, $arrayB:Array):Boolean 
{
	var isMatch:Boolean = true;
	if($arrayA.length == $arrayB.length) {
		for (var i:int = 0; i<$arrayA.length; i++) {
			if ($arrayA[i] != $arrayB[i]) {
				isMatch = false;
				break;
			}
		}
	} else {
		isMatch = false;
	}
	return isMatch;
}


trace(checkIfArraysMatch(array1, array2));

// OUTPUT: false

source

Sorting a Multi-Dimensional Array with PHP

function subval_sort($a,$subkey) {
	foreach($a as $k=>$v) {
		$b[$k] = strtolower($v[$subkey]);
	}
	asort($b);
	foreach($b as $key=>$val) {
		$c[] = $a[$key];
	}
	return $c;
}

source

Creating an array of objects to store atom rss data

<?php
error_reporting(E_ALL); // shows all errors, remove this on live sites

$feedURL = '<a href="http://mydomain.com/rss/example.xml';" >http://mydomain.com/rss/example.xml';</a>
$getFILE = get_content($feedURL) or die("feed not loading");
$xml = simplexml_load_string($getFILE, NULL, LIBXML_NOCDATA); // Set CDATA as text nodes
$xml->registerXPathNamespace('atom', '<a href="http://www.w3.org/2005/Atom');" >http://www.w3.org/2005/Atom');</a> // define namespace atom

    class oEntry
    {
        public $url;
        public $author;
        public $date;
        public $text;
        
        function oEntry($url, $author, $date, $text) {
        	$this->url = $url;
        	$this->author = $author;
        	$this->date = $date;
        	$this->text = $text;
        }
    } 

	foreach($xml->xpath('atom:entry') as $entry) {
		// array of objects
		$e[] = new oEntry($xml->link['href'], $entry->author->name, $entry->published, $entry->content); 	
	} // end foreach

?>

SAMPLE XML INPUT (you need to fill out the tags):
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="<a href="http://www.w3.org/2005/Atom"" >http://www.w3.org/2005/Atom"</a> xmlns:thr="<a href="http://purl.org/syndication/thread/1.0">" >http://purl.org/syndication/thread/1.0"></a>
  <link rel="alternate" type="text/html" href="" />
  <id></id>
  <updated></updated>
  <title></title>
  
  <entry>
    <id></id>
    <thr:in-reply-to ref="" type="text/html" href=""/>
    <link rel="alternate" type="text/html" href="" />
    <title></title>
    <author>
        <name></name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en-us" xml:base="">
        <![CDATA[<p>html text goes here</p>]]>
    </content>
    <published>February 20, 2010  2:23 AM</published>
  </entry>

</feed>

source

Verify if content of 2 lists/arrays are eaqual

var listsEqual = function(list1, list2){
    var differences = 0;
    // check if all elements from first list exists in the second
    for(var i=0; i<list1.length;i++){   
        var exists = false;
        for(var j=0; j<list2.length; j++){
            if(list1[i] == list2[j]){
                exists = true;
                break;
            }
        }
        if(!exists){
            differences++;
            break;
        }
    }
    // and vice versa
    if(differences == 0){
        for(var i=0; i<list2.length;i++){
            var exists = false;
            for(var j=0; j<list1.length; j++){
                if(list2[i] == list1[j]){
                    exists = true;
                    break;
                }
            }
            if(!exists){
                differences++;
                break;
            }
        }
    }
    return differences == 0;
}

source

orderMultiAssocArray

function orderMultiAssocArray($array=array(), $field='', $keep_assoc=true) {
	if ($field != '') {
		if (strstr($field, ' desc')) {
			$field = str_replace(' desc', '', $field);
			$reverse = true;
		}
		else {
			$reverse = false;
		}
		$ar = array();
		foreach ($array as $key => $a) {
			$ar[$key] = $a[$field];
		}
		if ($reverse) { arsort($ar); }
		else { asort($ar); }
		$array_redux = array();
		$counter=0;
		foreach ($ar as $k => $a) {
			$key = $keep_assoc ? $k : $counter;
			$array_redux[$key] = $array[$k];
			$counter++;
		}
		$array = $array_redux;
	}
	return $array;
}

source

Search within an Array

package com.blogspot.flanture.arrays
{

// search Array as3 functions
// author: <a href="http://flanture.blogspot.com" >http://flanture.blogspot.com</a> 
// contact: <a href="mailto:flanture@gmail.com">flanture@gmail.com</a>
// January 2010

	import flash.display.Sprite;

	public class SearchArray extends Sprite {
		
		public function SearchArray() {
			
			// example usage
			
			var a:Array = [1,2,3,5,3];
			var b:Array = [2,4,6,8];
			
			search(6, b);
			trace("a combined with b: "+combine(a,b));
			trace("6 exists in b? "+searchB(6,b));
			trace("number of 3 in a? "+searchCount(3,a));
			trace("intersection of a and b: "+iSection(a,b));
			trace("shuffled b: "+shuffle(b));			
		}

	// function search finds element in array and displays result		
		
	public static function search(word:Object, arr:Array):void {
			var exists:Boolean = false;
			for(var i:uint=0; i < arr.length; i++){
				if(arr[i]==word){
					trace("Element exist on position "+i);
					exists = true;
				}
			}
			if(!(exists)){
				trace("Element doesn't exist in array.");
			}
		}
		
	// function searchB finds element in array and returns Boolean value
		
	public static function searchB(word:Object, arr:Array):Boolean {
			var exists:Boolean = false;
			for(var i:uint=0; i < arr.length; i++) {
				if(arr[i]==word){
					exists = true;
				}
			}
			return exists;
		}
		
	// function searchCount returns number of apperances of element in array
		
	public static function searchCount(word:Object, arr:Array):uint {
			var counter:uint = 0;
			for(var i:uint=0; i < arr.length; i++) {
				if(arr[i]==word){
					counter+=1;
				}
			}
			return counter;
		}
		
	// function iSection returns intersection array of two arrays
		
	public static function iSection(arr1:Array, arr2:Array):Array {
			var arr3:Array = new Array();
			var count:uint = 0;
			for(var i:uint=0; i < arr1.length; i++){
				for(var j:uint=0; j < arr2.length; j++){
					if(arr1[i]==arr2[j]){
						arr3[count] = arr1[i];
						count+=1;
					}
				}
			}
			return arr3;
		}
		
	// function shuffle simply shuffles given array elements
		
	public static function shuffle(b:Array):Array {
			var temp:Array = new Array();
			var templen:uint;
			var take:uint;
			while (b.length > 0) {
				take = Math.floor(Math.random()*b.length);
				templen = temp.push(b[take]);
				b.splice(take,1);
			}
			return temp;
		}
		
	// function combine returns union of two arrays
	
	public static function combine(ar1:Array, ar2:Array):Array {
		var rAr:Array = new Array();
		var i:uint = 0;
		var j:uint = 0;
		while((i < ar1.length) || (j < ar2.length)) {
			if(i < ar1.length){
				rAr.push(ar1[i]);
				i+=1;
			}
			if(j < ar2.length){
				rAr.push(ar2[j]);
				j+=1;
			}
		}
		return rAr;
		}		
	}
}

source