This shows you the differences between two versions of the page.
| — |
articles:insensible-casse-array-php:start [2013/10/05 21:34] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | |||
| + | ====== Quelques fonctions insensibles à la casse pour manipuler des tableaux en PHP ====== | ||
| + | |||
| + | Tester si une chaîne est présente dans une liste : | ||
| + | <code php> | ||
| + | function in_iarray($str, $a) | ||
| + | { | ||
| + | foreach ($a as $v) { | ||
| + | if (strcasecmp($str, $v) == 0) { | ||
| + | return true; | ||
| + | } | ||
| + | } | ||
| + | return false; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Dédoublonner une liste de chaînes de caractères : | ||
| + | <code php> | ||
| + | function array_iunique($a) | ||
| + | { | ||
| + | $n = array(); | ||
| + | foreach ($a as $k => $v) { | ||
| + | if (!in_iarray($v, $n)) { | ||
| + | $n[$k] = $v; | ||
| + | } | ||
| + | } | ||
| + | return $n; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Soustraction entre deux listes de chaînes de caractères : | ||
| + | <code php> | ||
| + | function array_idiff($array1, $array2) | ||
| + | { | ||
| + | foreach ($array1 as $key=>$element) { | ||
| + | foreach($array2 as $key2=>$element2) { | ||
| + | if (strtolower($element) == strtolower($element2)) { | ||
| + | unset($array1[$key]); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | return $array1; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | |||
| + | {{tag>article computing php array}} | ||
| + | ~~DISCUSSION~~ | ||