Tag Archive for associative

Array Search on Key

function array_search_key($needle, $haystack, $haystackKey, $strict = false) {
	foreach($haystack as $key => $value) {
		if(isset($value[$haystackKey])) {
			if($strict) {
				if($value[$haystackKey] === $needle) return true;
			} else {
				if($value[$haystackKey] == $needle) return true;
			}
		}
	}
	
	return false;
}

source

get first key of associative array

echo key($array);

source

get first value of an associative array

echo reset($array);

source

Associative Array – advance

var AssociativeArray = function() {

this.hash = new Array();

this.size = function() {
var size = 0;
for(var i in this.hash) {
if(this.hash[i] != null) {
size++;
}
}
return size;
};

this.clear = function() {
this.hash = new Array();
};

this.add = function(key, value) {
if(key == null || value == null) {
throw "NullPointerException { "" + key + "" : "" + value + "" }";
}
else {
this.hash[key] = value;
}
};

this.get = function(key) {
return this.hash[key];
};

this.remove = function() {
var value = this.hash[key];
this.hash[key] = null;
return value;
};

this.contains_key = function(key) {
var exist = false;
for(var i in this.hash) {
if(i == key && this.hash[i] != null) {
exist = true;
break;
}
}
return exist;
};

this.contains_value = function(value) {
var contains = false;
if(value != null) {
for(var i in this.hash) {
if(this.hash[i] == value) {
contains = true;
break;
}
}
}
return contains;
};

this.is_empty = function() {
return (parseInt(this.size()) == 0) ? true : false;
};

this.keys = function() {
var keys = new Array();
for(var i in this.hash) {
if(this.hash[i] != null) {
keys.push(i);
}
}
return keys;
};

this.values = function() {
var values = new Array();
for(var i in this.hash) {
if(this.hash[i] != null) {
values.push(this.hash[i]);
}
}
return values;
};

this.to_string = function() {
var string = "{"array": [
";
for(var i in this.hash) {
if(this.hash[i] != null) {
string += "	{ "" + i + "" : "" + this.hash[i] + "" },
";
}
}
return string += "]}";
};
}

source

php define typoscript from array of associative arrays

//build CONTENT objects
foreach ($items as $item) {

$newItem = ($x+1)*10;
$conf['10.'][$newItem] = 'CONTENT';
$conf['10.'][$newItem.'.'] = $dummyItem;
$conf['10.'][$newItem.'.']['select.']['uidInList'] = $item['uid'];
$conf['10.'][$newItem.'.']['select.']['pidInList'] = $item['pid'];
if($item['lock']) {$conf['10.'][$newItem.'.']['renderObj.']['stdWrap.']['dataWrap'] = $lConf['lockItemWrap'];} // special wrap to make item non-draggable and for providing custom CSS
$x++;

}

source

php while statement associative array from mysql

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

source

filterXMLAssocArray

// Filters specified tags from a supplied XML Node resource by tag name.
// Returns a new associative array with the filtered tags' attributes.
private function filterXMLAssocArray(filterNodeName:String, xmlNodes:Array):Array{

var tmpArray:Array = new Array();

var indexCounter:Number = 0;
for (var i:Number = 0; i<xmlNodes.length; i++){
if (xmlNodes[i].nodeName == filterNodeName){
tmpArray[indexCounter] = new Array();
tmpArray[indexCounter] = xmlNodes[i].attributes;
indexCounter ++;
}
}
return tmpArray;
};

source

Flash Window ActionScript Functionality

//Create a global variable to hold the current top depth available
var nTopDepth:Number;
//Create an associative array to store the names of the opened windows
var oOpenedWindows:Object = new Object();

//Add the 2 event handles to the buttons. Passes the id name through to the addWindow function.
mcButtonOne.onRelease = function():Void {
addWindow("WindowOne");
};
mcButtonTwo.onRelease = function():Void {
addWindow("WindowTwo");
};

function addWindow(sLinkage:String):Void {
//If a value already exists in the associative array for the window, return.
if(oOpenedWindows[sLinkage] != undefined) {
oOpenedWindows[sLinkage].swapDepths(nTopDepth);//if the window exists, bring the existing window to the top depth
return;
}
var nIndex:Number = this.getNextHighestDepth();//get the next highest level to put the window on
var mcWindow:MovieClip = this.attachMovie(sLinkage, "mcWindow" + nIndex, nIndex);//attach the selected window and apply the next highest depth
mcWindow._x = 100;//set selected window to 100px
mcWindow._y = 100;//set selected window to 100px
nTopDepth = nIndex;//set the nTopDepth to nIndex we aquired above.
oOpenedWindows[sLinkage] = mcWindow;//Insert a value into the associative array

mcWindow.mcWindowBackground.onPress = function():Void {
this._parent.swapDepths(nTopDepth);//swap the selected window with the current top depth to bring it to the front
this._parent.startDrag();//target the window background, then its parent to add a drag function onPress
};
mcWindow.mcWindowBackground.onRelease = function():Void {
this._parent.stopDrag();//target the window background, then its parent to remove a drag function onRelease
};
mcWindow.mcWindowBackground.onReleaseOutside = mcWindow.mcWindowBackground.onRelease;

mcWindow.mcCloseSquare.onRelease = function():Void {
delete oOpenedWindows[sLinkage]//remove the window from the array on close
this._parent.removeMovieClip();//remove the movie clip when you press the close button.
};
}

source