* @copyright 2025 Bartolomé Sintes Marco * @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later * @version 2025-02-08 * @link https://www.mclibre.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ ?> Función miArrayReverse(). Funciones (1). Sin formularios. Ejercicios. PHP. Bartolomé Sintes Marco. www.mclibre.org

Función miArrayReverse()

Actualice la página para mostrar una nueva matriz.

$value]; foreach ($array as $indice => $valor) { $m[$indice] = $valor; } return $m; } function miArrayReverse(array $array, bool $preserve_keys = false): array { if ($preserve_keys) { $array2 = []; foreach ($array as $indice => $valor) { $array2 = insertaValorMatriz($array2, $indice, $valor); } } else { $array = array_values($array); $n = count($array); for ($i = 0; $i < $n; $i++) { $array2[$i] = $array[$n - $i - 1]; } } return $array2; } $n = rand(7, 10); $m = generaMatrizEnterosRandRand($n, 1, 10); print "

Matriz de $n valores enteros

\n"; print "\n"; print "
\n";
print_r($m);
print "
\n"; print "\n"; $m2 = miArrayReverse($m, true); print "

La misma matriz, al revés y manteniendo los índices

\n"; print "\n"; print "
\n";
print_r($m2);
print "
\n"; print "\n"; $m2 = miArrayReverse($m); print "

La misma matriz, al revés, pero sin mantener los índices

\n"; print "\n"; print "
\n";
print_r($m2);
print "
\n"; print "\n"; ?>