Last active
January 2, 2024 03:16
-
-
Save loschke/6b2d7c6771067ab8390f9e7fea08b8ca to your computer and use it in GitHub Desktop.
Code Snippets zum Blogbeitrag https://sevenx.de/php-array-teil-2-nuetzliche-funktionen-zur-effektiven-arbeit-mit-arrays/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isAssocArray($array){ | |
return array_keys($array) !== range(0, count($array) - 1); | |
} | |
$mein_array = array("DE" => "Deutschland", "EN" => "England"); //Beispielarray | |
//Prüfung und Ausgabe | |
echo (isAssocArray($mein_array) ? "isAssoc" : "false"); //gibt isAssoc aus | |
$mein_array = array("Deutschland", "England"); //Beispielarray | |
//Prüfung und Ausgabe | |
echo (isAssocArray($mein_array) ? "isAssoc" : "false"); //gibt false aus | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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"); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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, "list-inline"); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isMultiArray($array) { | |
return ((count($array) != count($array, 1)) ? true : false); | |
} | |
$mein_array = array("Deutschland", "England"); //Beispielarray | |
//Prüfung und Ausgabe | |
echo (isMultiArray($mein_array) ? "isMulti" : "false"); // gibt false aus | |
$mein_array = array("DE" => "Deutschland", "GB" => array("Schottland", "England", "Wales", "Nordirland")); //Beispielarray | |
//Prüfung und Ausgabe | |
echo (isMultiArray($mein_array) ? "isMulti" : "false"); // gibt isMulti aus | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function arrayHasValue ($array, $value) { | |
return (in_array($value, $array) ? true : false); | |
} | |
$mein_array = array("Deutschland", "England"); //Beispielarray | |
//Prüfung und Ausgabe | |
echo (arrayHasValue($mein_array, "Deutschland") ? "hasValue" : "noValue"); //gibt hasValue aus | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function arrayHasKey ($array, $key) { | |
return (array_key_exists($key, $array) ? true : false); | |
} | |
$mein_array = array("DE" => "Deutschland", "EN" => "England"); //Beispielarray | |
//Prüfung und Ausgabe | |
echo (arrayHasKey($mein_array, "EN") ? "isKey" : "noKey"); //gibt isKey aus | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function addValue($array, $value, $key = NULL){ | |
if(!is_null($key)) { | |
$array[$key] = $value; | |
} else { | |
$array[] = $value; | |
} | |
return $array; | |
} | |
$mein_array = array("Deutschland", "England"); //Beispielarray | |
$mein_array = addValue($mein_array, "Kanada"); //Funktionsaufruf | |
//Ausgabe | |
print_r($mein_array); | |
//ergibt: Array ( [0] => Deutschland [1] => England [2] => Kanada ) | |
$mein_array = array("DE" => "Deutschland", "EN" => "England"); //Beispielarrays | |
$mein_array = addValue($mein_array, "Kanada", "CA"); //Funktionsaufruf | |
$mein_array = addValue($mein_array, "Schweiz", "CH"); //Funktionsaufruf | |
//Ausgabe | |
print_r($mein_array); | |
//ergibt: Array ( [DE] => Deutschland [EN] => England [CA] => Kanada [CH] => Schweiz ) | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function addArray($array, $new_array){ | |
return(array_merge($array, $new_array)); | |
} | |
$mein_array = array("DE" => "Deutschland", "EN" => "England"); //Beispielarray | |
$new_array = array("CA" => "Kanada", "CH" => "Schweiz"); //Plus Arrays | |
//Funktionsaufruf inkl. Ausgabe | |
print_r(addArray($mein_array, $new_array)); | |
//ergibt: Array ( [DE] => Deutschland [EN] => England [CA] => Kanada [CH] => Schweiz ) | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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"); //Beispielarray | |
$del_array = array("DE" => "Deutschland","CH" => "Schweiz"); // Minus Array | |
//Funktionsaufruf mit Ausgabe | |
print_r(deleteByValue($array, $del_array)); | |
//ergibt: Array ( [EN] => England [CA] => Kanada ) | |
$array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz"); //Beispielarray | |
$value = "England"; // Minus Value | |
//Funktionsaufruf mit Ausgabe | |
print_r(deleteByValue($array, $value)); | |
//ergibt: Array ( [DE] => Deutschland [CA] => Kanada [CH] => Schweiz ) | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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"); //Beispielarray | |
$key_array = array("CH", "CA"); // Minus Key Array | |
//Funktionsaufruf mit Ausgabe | |
print_r(deleteByKey($mein_array, $key_array)); | |
//ergibt: Array ( [DE] => Deutschland [EN] => England ) | |
$mein_array = array("DE" => "Deutschland","EN" => "England","CA" => "Kanada","CH" => "Schweiz"); //Beispielarray | |
$del_key = "CA"; // Minus Key | |
//Funktionsaufruf mit Ausgabe | |
print_r(deleteByKey($mein_array, $del_key)); | |
//ergibt: Array ( [DE] => Deutschland [EN] => England [CH] => Schweiz ) | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment