Created
September 16, 2025 12:08
-
-
Save Robdebert/5657af10c0d88754046bf864bbb738a7 to your computer and use it in GitHub Desktop.
Wandelt geladene Datensätze so um, dass sie assoziativ über die ID zugreifbar sind.
This file contains hidden or 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
| /** | |
| * Wandelt geladene Datensätze so um, dass sie assoziativ über die ID zugreifbar sind. | |
| * Welche ID es ist, wird mit dem Parameter $key_field_name festgelegt | |
| * $flat = true bewirkt, dass Einträge zu $return[$id] hinzugefügt werden (zB $return[$id][0] und $return[$id][1], usw). | |
| * | |
| * @param array $rows | |
| * @param string $key_field_name | |
| * @param string $flat_field_name String oder Array von Strings (PM, RH seit 26.03.2014) | |
| * @return array | |
| */ | |
| if( !function_exists("create_select_array")) { | |
| function create_select_array($rows, $key_field_name, $flat = true, $flat_field_name="") { | |
| $return = array(); | |
| if (empty($key_field_name)) { | |
| preprint("key_field_name darf nicht leer sein!", __FILE__.__LINE__); | |
| } | |
| if(!empty($rows) && (is_array($rows) || is_object($rows))) { // auch stdClass Objekte können jetzt hiermit durchlaufen und umgeformt werden. PM RH 08.10.20 | |
| foreach ($rows as $row) { | |
| // Handelt es sich um ein Objekt, statt ein Array? | |
| if (is_object($row)) { | |
| if (!isset($row->$key_field_name)) { | |
| enqueueMessage("$key_field_name nicht in \$row gefunden", "debug"); | |
| continue; | |
| } | |
| $id = $row->$key_field_name; | |
| $flat_value = ""; | |
| if (is_array($flat_field_name)) { | |
| $flat_value = array(); | |
| foreach ($flat_field_name as $ffn) { | |
| if (isset($row->$ffn)) { | |
| $flat_value[] = $row->$ffn; | |
| } | |
| } | |
| $flat_value = implode(" // ", $flat_value); | |
| } else { | |
| if (isset($row->$flat_field_name)) { | |
| $flat_value = $row->$flat_field_name; | |
| } | |
| } | |
| } | |
| // Ist $row ein Array und kein Objekt? | |
| if (is_array($row)) { | |
| if (!isset($row[$key_field_name])) { | |
| enqueueMessage("$key_field_name nicht in \$row gefunden", "debug"); | |
| continue; | |
| } | |
| $id = $row[$key_field_name]; | |
| $flat_value = ""; | |
| if (is_array($flat_field_name)) { | |
| $flat_value = array(); | |
| foreach ($flat_field_name as $ffn) { | |
| if (isset($row[$ffn])) { | |
| $flat_value[] = $row[$ffn]; | |
| } | |
| } | |
| $flat_value = implode(" // ", $flat_value); | |
| } else { | |
| if (isset($row[$flat_field_name])) { | |
| $flat_value = $row[$flat_field_name]; | |
| } | |
| } | |
| } | |
| // Achtung: das "&" ist scheinbar als Key nicht erlaubt. $clients["Deffner & Johann"] liefert nichts zurück | |
| // $id = str_replace("&", "_", $id); | |
| if ($flat) { | |
| if ($flat_field_name!=="") $return[$id] = $flat_value; | |
| else $return[$id] = $row; | |
| } | |
| else { | |
| if (!isset($return[$id])) $return[$id] = array(); | |
| $return[$id][] = $row; | |
| } | |
| } | |
| } | |
| return $return; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment