Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nützliche Funktionen zur Arbeit mit Array
<?php
//check if an array is associative
function isAssocArray($array){
return array_keys($array) !== range(0, count($array) - 1);
}
//check if an array is multidimensional
function isMultiArray($array) {
return ((count($array) != count($array, 1)) ? true : false);
}
//check if a value is in array
function arrayHasValue ($array, $value) {
return (in_array($value, $array) ? true : false);
}
//check if a key is in array
function arrayHasKey ($array, $key) {
return (array_key_exists($key, $array) ? true : false);
}
//add single value
function addValue($array, $value, $key = NULL){
if(!is_null($key)) {
$array[$key] = $value;
} else {
$array[] = $value;
}
return $array;
}
//add array
function addArray($array, $new_array){
return(array_merge($array, $new_array));
}
//delete value by value
function deleteByValue($array, $values)
{
if(is_array($values)) {
return(array_diff($array, $values));
} else {
return(array_diff($array, array($values)));
}
}
//delete value by key
function deleteByKey($array, $keys)
{
if(is_array($keys)) {
foreach ($keys as $key) {
unset($array[$key]);
}
return($array);
} else {
unset($array[$keys]);
return($array);
}
}
// get value by key
function getValueByKey($array, $key)
{
return array_key_exists($key, $array) ? $array[$key] : null;
}
// get key by value
function getKeyByValue($array, $value) {
return array_search($value, $array);
}
// list values as html list
function listValues($array, $class = NULL) {
return("<ul class='{$class}'><li>".implode("</li><li>",$array)."</li></ul>");
}
//list keys as html list
function listKeys($array, $class = NULL) {
return("<ul class='{$class}'><li>".implode("</li><li>",array_keys($array))."</li></ul>");
}
//create HTML definition list form assoc array
function defListArray($array, $class = NULL) {
$list = "<dl class='{$class}'>";
foreach ($array as $key => $value) {
$list .= "<dt>{$key}</dt>";
$list .= "<dd>{$value}</dd>";
}
$list .= "</dl>";
return($list);
}
// nested HTML list from two dimensional array
function nestedList ($array, $class = NULL) {
$list = "<ul class='{$class}'>";
foreach ($array as $key => $value) {
$list .= "<li>{$key}";
$list .= "<ul>";
foreach($value as $data_key => $data_value) {
$list .= "<li>{$data_key} - {$data_value}</li>";
}
$list .= "</ul></li>";
}
$list .= "</ul>";
return($list);
}
//count array elements
function countArray($array) {
return(count($array));
}
?>
<?php
$mein_array = array(); // Array initialisieren
// TRUE / FALSE Abfragen
//###############################
//Prüfen ob Array is Assoziativer
function isAssocArray($array){
return array_keys($array) !== range(0, count($array) - 1);
}
$mein_array = array("DE" => "Deutschland", "EN" => "England");
echo (isAssocArray($mein_array) ? "isAssoc" : "false"); //gibt isAssoc aus
$mein_array = array("Deutschland", "England");
echo (isAssocArray($mein_array) ? "isAssoc" : "false"); //gibt false aus
//Prüfen ob Multidimensionaler Array
function isMultiArray($array) {
return ((count($array) != count($array, 1)) ? true : false);
}
$mein_array = array("Deutschland", "England");
echo (isMultiArray($mein_array) ? "isMulti" : "false"); // gibt false aus
$mein_array = array("DE" => "Deutschland", "GB" => array("Schottland", "England", "Wales", "Nordirland"));
echo (isMultiArray($mein_array) ? "isMulti" : "false"); // gibt isMulti aus
//Prüfen ob Value in Array vorhanden ist
function arrayHasValue ($array, $value) {
return (in_array($value, $array) ? true : false);
}
$mein_array = array("Deutschland", "England");
echo (arrayHasValue($mein_array, "Deutschland") ? "hasValue" : "noValue"); //gibt hasValue aus
//Prüfen ob Key in Array vorhanden ist
function arrayHasKey ($array, $key) {
return (array_key_exists($key, $array) ? true : false);
}
$mein_array = array("DE" => "Deutschland", "EN" => "England");
echo (arrayHasKey($mein_array, "EN") ? "isKey" : "noKey"); //gibt isKey aus
// Array Werte hinzufügen/löschen
//###############################
//Einzelnen Wert hinzufügen
function addValue($array, $value, $key = NULL){
if(!is_null($key)) {
$array[$key] = $value;
} else {
$array[] = $value;
}
return $array;
}
$mein_array = array("Deutschland", "England");
$mein_array = addValue($mein_array, "Kanada");
print_r($mein_array);
$mein_array = array("DE" => "Deutschland", "EN" => "England");
$mein_array = addValue($mein_array, "Kanada", "CA");
$mein_array = addValue($mein_array, "Schweiz", "CH");
print_r($mein_array);
//Mehrere Werte in als Array hinzufügen
$mein_array = array("DE" => "Deutschland", "EN" => "England");
$new_array = array("CA" => "Kanada", "CH" => "Schweiz");
function addArray($array, $new_array){
return(array_merge($array, $new_array));
}
print_r(addArray($mein_array, $new_array));
//Elemente via Value aus Array löschen
function deleteByValue($array, $values)
{
if(is_array($values)) {
return(array_diff($array, $values));
} else {
return(array_diff($array, array($values)));
}
}
$array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
$del_array = array("DE" => "Deutschland","CH" => "Schweiz");
print_r(deleteByValue($array, $del_array));
$array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
$value = "England";
print_r(deleteByValue($array, $value));
function deleteByKey($array, $keys)
{
if(is_array($keys)) {
foreach ($keys as $key) {
unset($array[$key]);
}
return($array);
} else {
unset($array[$keys]);
return($array);
}
}
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
$key_array = array("CH", "CA");
print_r(deleteByKey($mein_array, $key_array));
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
$del_key = "CA";
print_r(deleteByKey($mein_array, $del_key));
// Array Werte ausgeben
//###############################
// Value zu bestimmten Key ausgeben
function getValueByKey($array, $key)
{
return array_key_exists($key, $array) ? $array[$key] : null;
}
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
echo getValueByKey($mein_array, "CA");
// get key by value
function getKeyByValue($array, $value) {
return array_search($value, $array);
}
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
echo getKeyByValue($mein_array, "Schweiz");
// HTML Liste aus Array Values
function listValues($array, $class = NULL) {
return("<ul class='{$class}'><li>".implode("</li><li>",$array)."</li></ul>");
}
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
echo listValues($mein_array);
//HTML Liste aus Array Keys
function listKeys($array, $class = NULL) {
return("<ul class='{$class}'><li>".implode("</li><li>",array_keys($array))."</li></ul>");
}
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
echo listKeys($mein_array, "inline");
//Definition Liste inkl. Keys und Values
function defListArray($array, $class = NULL) {
$list = "<dl class='{$class}'>";
foreach ($array as $key => $value) {
$list .= "<dt>{$key}</dt>";
$list .= "<dd>{$value}</dd>";
}
$list .= "</dl>";
return($list);
}
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
echo defListArray($mein_array);
// Verschachtelte HTML Liste für zweidimensionale assoc Arrays
function nestedList ($array, $class = NULL) {
$list = "<ul class='{$class}'>";
foreach ($array as $key => $value) {
$list .= "<li>{$key}";
$list .= "<ul>";
foreach($value as $data_key => $data_value) {
$list .= "<li>{$data_key} - {$data_value}</li>";
}
$list .= "</ul></li>";
}
$list .= "</ul>";
return($list);
}
$mein_array = array(
"England" => array(
"Code" => "EN",
"Capital" => "London",
"Currency" => "Pfund",
"Continent" => "Europoa",
"Language" => "Englisch"
),
"Deutschland" => array(
"Code" => "DE",
"Capital" => "Berlin",
"Currency" => "Euro",
"Continent" => "Europoa",
"Language" => "Deutsch"
),
"Kanada" => array(
"Code" => "CA",
"Capital" => "Ottawa",
"Currency" => "Kanad. Dollar",
"Continent" => "Nordamerika",
"Language" => "englisch"
)
);
echo nestedList($mein_array);
//Elemente im Array Zählen
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz");
function countArray($array) {
return(count($array));
}
echo countArray($mein_array);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.