PHP :: Aufgabe #282 :: Lösung #2
2 Lösungen
#282
Mehrdimensional zu Eindimensional
Anfänger - PHP
von Exception
- 11.05.2020 um 08:38 Uhr
Das folgende mehrdimensionale Array soll zu einem flachen, eindimensionalen Array umgewandelt werden. Viel Spaß.
PHP-Code
$a = [ 0 => 'Text 0', 1 => 'Text 1', 2 => 'Text 2', 'A' => [ 0 => 'Text A 0', 1 => 'Text A 1', 2 => 'Text A 2', ], 'B' => [ 'A' => [ 0 => 'Text B A 0', 1 => 'Text B A 1', 2 => 'Text B A 2', ] ] ];
#2
von Exception (7090 Punkte)
- 28.08.2020 um 13:41 Uhr
PHP-Code
$a = [ 0 => 'Text 0', 1 => 'Text 1', 2 => 'Text 2', 'A' => [ 0 => 'Text A 0', 1 => 'Text A 1', 2 => 'Text A 2', ], 'B' => [ 'A' => [ 0 => 'Text B A 0', 1 => 'Text B A 1', 2 => 'Text B A 2', ] ] ]; print_r(flattenMultidimensionalArray($a));
PHP-Code
function flattenMultidimensionalArray(array $haystack): array { $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack)); return iterator_to_array($iterator, false); }
After method call
Konsolenausgabe:
Array
(
[0] => Text 0
[1] => Text 1
[2] => Text 2
[3] => Text A 0
[4] => Text A 1
[5] => Text A 2
[6] => Text B A 0
[7] => Text B A 1
[8] => Text B A 2
)
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1